私は単体テストを学ぼうとしています。asp.net mvc 1.0で作成しているメンバーシップの一部を単体テストしようとしています。私は MVC に関する本を読んでいて、うまくいけば誰かが私のために片付けてくれるものについて混乱しています。
フレームワークに Nunit と Moq を使用しています。
質問1:
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」でmoqを使用していないということです。代わりにインターフェースを作成します
public interface IFormsAuthentication
{
void SetAuthCookie(string userName, bool createPersistentCookie);
void SignOut();
}
次に、ラッパーを作成します
public class FormsAuthenticationWrapper : IFormsAuthentication { public void SetAuthCookie(string userName, bool createPersistentCookie) { FormsAuthentication.SetAuthCookie(userName, createPersistentCookie); } public void SignOut() { FormsAuthentication.SignOut(); }
}
そして最後にプロパティ
public IFormsAuthentication FormsAuth
{
get;
private set;
}
メンバーシップと同様に、彼らだけが持っています
public static MembershipProvider プロバイダー { get; プライベートセット; }
何を変更するのかもわかりません。この行も何に変更しますか?
フォーム認証 = フォーム認証 ?? 新しい 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();
}
実際は常に null に戻ります。ViewResult、RedirectResult、RedirectToRouteResult を試しましたが、全員が null に戻ります。最初に奇妙だと思うので、なぜこれが起こっているのかわかりません
FormsAuth.RedirectFromLoginPage(loginValidation.UserName, rememberMe);
ビューを停止せず、リダイレクトを開始します。最初は、この行に到達すると return ステートメントのようなもので、他のコードは実行されないと思いましたが、そうではないように見えるので、これが問題になるかどうかはわかりません。
ありがとう