我正在尝试学习单元测试。我正在尝试对我在 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();
}
然后做一个包装器
公共(public)类 FormsAuthenticationWrapper : IFormsAuthentication { public void SetAuthCookie(string userName, bool createPersistentCookie) { FormsAuthentication.SetAuthCookie(用户名, createPersistentCookie); } 公共(public)无效注销() { FormsAuthentication.SignOut();
}
最后是属性
public IFormsAuthentication FormsAuth
{
get;
private set;
}
他们只有成员(member)资格
public static MembershipProvider 提供者 { 得到; 私有(private)集;
我也不确定要更改哪些内容。就像我也要更改此行一样?
FormsAuth = formsAuth ??新的 FormsAuthenticationWrapper();
我还尝试将另一种方法添加到 FormsAuthentication 接口(interface)和包装器中。
public void RedirectFromLoginPage(string userName, bool createPersistentCookie) { FormsAuthentication.RedirectFromLoginPage(用户名, 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();
}
Actual 总是返回 null。我尝试了 ViewResult、RedirectResult 和 RedirectToRouteResult,但每个人都返回 null。所以我不确定为什么会这样,因为我首先发现它很奇怪
FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
不停止 View 并开始重定向。一开始我以为一旦它到达这一行就好像一个返回语句并且不会执行其他代码但是 htis 似乎不是这种情况所以我不确定这是否是问题所在。
谢谢
最佳答案
问题1
??
称为 null-coalescing operator ,并且是 C# 2.0 以后的一个非常有用的特性。
在你的情况下,
FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
只是表示“将 formsAuth
分配给 FormsAuth
除非它为空,在这种情况下分配 new FormsAuthenticationWrapper()
”。它基本上是一种防止代码中出现空引用的方法。您也可以将其视为以下条件表达式的快捷方式:
FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();
问题2
this(null, null)
的使用调用constructor chaining .所有这一切意味着同一个类中的构造函数(因此 this
,而不是父类的 base
)接受两个参数,应该在主体之前被调用构造函数的执行。
重载构造函数是一种常见做法,可以让开发人员在只想使用默认属性/设置时更轻松地创建新对象。
问题3
正如其他人所提到的,这确实属于一个单独的问题。与前两者不同,它更具体地针对上下文/您的代码,而不是 C# 的语言特性。
更新
好吧,我现在所做的实际上是重写了这里的两个构造函数,因为我认为将它们放在另一种(几乎等效的)形式中可能会更清晰一些,并且可能也是更好的设计实践。此处不需要空合并运算符。
public AuthenticationController()
: this(new FormsAuthenticationWrapper(), Membership.Provider)
{
}
public AuthenticationController(IFormsAuthentication formsAuth,
MembershipProvider provider)
{
this.FormsAuth = formsAuth;
this.Provider = provider;
}
在这种形式中,很明显,采用两个参数的构造函数只是将类变量分配给参数的值。无参数构造函数(通常称为 default 构造函数)只是使用 default FormsAuth
和 Provider
对象创建一个新对象,它们是通过构造函数链指定的。
关于asp.net-mvc - 需要帮助理解这段代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1027054/