【发布时间】:2010-11-04 20:26:23
【问题描述】:
我正在尝试学习单元测试。我正在尝试对我在 asp.net mvc 1.0 中制作的一些 Memembership 内容进行单元测试。我一直在关注一本关于 MVC 的书,我对一些希望有人可以为我澄清的东西感到困惑。
我在我的框架中使用 Nunit 和 Moq。
问题一:
public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider)
{
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
Provider = provider ?? Membership.Provider;
}
我有点困惑什么“??”我以前从未真正见过它。就像我什至不知道这里到底发生了什么。就像他们传递界面然后“??”标记发生并制作了一个新的 FormsAuthenticationWraper?
问题 2。
public AuthenticationController(): this(null, null)
{
}
我知道这是默认构造函数,但我不确定为什么 ":this(null,null)" 这样做。
比如它实现了什么?这也指的是什么。最重要的是,为什么不能把它排除在外?并保持默认构造函数不变。
问题 3。
在这本书(asp.net mvc 1.0 快速)中,它谈到了如何实现 Memembership 提供程序需要大量工作。所以他们使用 moq 模型框架来让生活更轻松。
现在我的问题是他们不使用“FormsAuthentication”上的起订量。他们反而制作了一个界面
public interface IFormsAuthentication
{
void SetAuthCookie(string userName, bool createPersistentCookie);
void SignOut();
}
然后做一个包装器
公共类 FormsAuthenticationWrapper : IFormsAuthentication { 公共无效 SetAuthCookie(字符串用户名,布尔 createPersistentCookie) { FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); } 公共无效SignOut() { FormsAuthentication.SignOut(); }
}
最后是一个属性
public IFormsAuthentication FormsAuth
{
get;
private set;
}
与他们只有会员资格一样
公共静态 MembershipProvider 提供者 { 得到; 私人套装; }
我也不确定要更改哪些内容。就像我也会改变这条线?
FormsAuth = formsAuth ??新的 FormsAuthenticationWrapper();
我还尝试将另一种方法添加到 FormsAuthentication 接口和包装器中。
public void RedirectFromLoginPage(string userName, bool createPersistentCookie) { FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie); }
但我不确定发生了什么,但我的单元测试总是失败,这与我尝试修复它无关。
public ActionResult Login(string returnUrl, FormCollection form, bool rememberMe)
{
LoginValidation loginValidation = new LoginValidation();
try
{
UpdateModel(loginValidation, form.ToValueProvider());
}
catch
{
return View("Login");
}
if (ModelState.IsValid == true)
{
bool valid = authenticate.VerifyUser(loginValidation.UserName, loginValidation.Password);
if (valid == false)
{
ModelState.AddModelError("frm_Login", "Either the Password or UserName is invalid");
}
else if (string.IsNullOrEmpty(returnUrl) == false)
{
/* if the user has been sent away from a page that requires them to login and they do
* login then redirect them back to this area*/
return Redirect(returnUrl);
}
else
{
FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
}
}
return View("Login");
Here is my test
[测试] 公共无效 Test_If_User_Is_Redirected_Back_To_Page_They_Came_From_After_Login() { System.Diagnostics.Debugger.Break();
var formsAuthenticationMock = new Mock<AuthenticationController.IFormsAuthentication>();
var membershipMock = new Mock<MembershipProvider>();
membershipMock.Setup(m => m.ValidateUser("chobo2", "1234567")).Returns(true);
// Setup controller
AuthenticationController target = new AuthenticationController(formsAuthenticationMock.Object, membershipMock.Object);
// Execute
FormCollection form = new FormCollection();
form.Add("Username", "chobo2");
form.Add("password", "1234567");
ViewResult actual = target.Login(null, form, false) as ViewResult;
Assert.That(actual.View, Is.EqualTo("home"));
formsAuthenticationMock.Verify();
}
Actual 总是返回 null。我尝试了 ViewResult、RedirectResult 和 RedirectToRouteResult,但每个人都返回 null。所以我不确定为什么会发生这种情况,因为我首先觉得很奇怪
FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
不停止视图并开始重定向。起初我以为一旦它到达这一行,它就像一个 return 语句,那就是它不会执行其他代码,但 htis 似乎不是这种情况,所以我不确定这是否是问题。
谢谢
【问题讨论】:
-
你应该把你的问题分成不同的帖子。问题 1 和 2 是相关的,可以是一篇文章,但问题 3 很可能是独立的。
-
问题3真的不止一个问题
标签: asp.net-mvc unit-testing nunit moq