我正在尝试学习单元测试。我正在尝试对我在 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 提供程序将是很多工作。所以他们使用最小起订量模型框架让生活更轻松。
现在我的问题是他们不使用“FormsAuthentication”上的起订量。他们改为制作一个界面
public interface IFormsAuthentication
{
void SetAuthCookie(string userName, bool createPersistentCookie);
void SignOut();
}
然后做一个包装
公共类 FormsAuthenticationWrapper : IFormsAuthentication { public void SetAuthCookie(string userName, bool createPersistentCookie) { FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); } public void 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
[测试] public void 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();
}
实际总是返回为空。我尝试了 ViewResult、RedirectResult 和 RedirectToRouteResult,但每个人都返回 null。所以我不确定为什么会发生这种情况,因为我首先觉得很奇怪
FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
不停止视图并开始重定向。起初我以为一旦它到达这一行,它就像一个返回语句,那就是它不会执行其他代码,但 htis 似乎不是这种情况,所以我不确定这是否是问题所在。
谢谢