asp.net - NUnit test case for mvc login -


What type of test case can we write for the following login controller in asp.net mvc

  [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login (LoginModel model, string returnUrl) {if (ModelState.IsValid & amp; & amp; WebSecurity.Login (model.UserName, model.PasswordpersistCookie: model.RememberMe )) {Return RedirectToLocal (returnUrl); } ModelState.AddModelError ("", "The username or password provided is incorrect."); See Return (Model); }   

1) You can verify that WebSecurity.Login with the correct parameters Is called when the model is valid

2) When WebSecurity.Login returns the correct returns, you can verify that you are redirected to reutnrUrl

3) if If the model is not valid then you can verify that your error in ModelState and refer back to the Result.

You have to reactivate the website to call the inquiries, you have to duplicate it. Probably have to use dependency injection technology.

Example

1) You have to hide your web security behind an interface and it must be used only through that interface (I think your WebSecurity Fixed class comes from WebMatrix.WebData namespace). Public Interface IWebSecurityHelper {bool Login (String Username, String Password, bool isPersistent = false)} // Security Assistant using WebMatrix.WebData implementation of your web; Public class WebSecurityHelper: IWebSecurityHelper {public bool login (string username, string password, bool is PERSESTENT = false) {WebSecurity.Login (username, password, isPersistent); }}

2) you in your controller will make an example of WebSecurityHelper, usually it is done using the IoC framework or but for this example I just WebSecurityHelper the controllers constructor I will initialize, which will still allow me to make fake injection for testing.

  Public class LoginController {Private Redondly IWebSecurityHelper _helper; Public Login Controller (IWebSecurityHelper Assistant) {_helper = Assistant; } // passing its implementation WebSecurityHelper public LoginController (): this (new WebSecurityHelper ()) {} [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login (LoginModel model, string returnUrl) {if (ModelState.IsValid & amp; & Amp; _helper.Login (model.UserName, model.PasswordpersistCookie: model.RememberMe)) {Return Redirect Local (returnUrl); } ModelState.AddModelError ("", "The username or password provided is incorrect."); See Return (Model); }}   

3) In your unit test, you must duplicate IWebSecurityHelper. There are many funky frameworks, I personally prefer.

  [TestFixture] public class LoginControllerTests {// this test is that your controller successfully WebSecurityHelper login method correct parameter [Test] public void LoginAction_Must_Call_WebSecurityLogin () is called with {var loginModel = new LoginModel Will confirm () {UserName = "test", password = "test"} helper hoc = new fake & lt; IWebSecurityHelper & gt; (); HelperMock.Expect (m = & gt; m.Login (LoginModel.name, LoginModel.password)); Var Controller = New Login Controller (_helperMock.Object); Administrator.login (login modell, string.optim); Helpermock.Verify (m = & gt; m.Login (LoginModel.Username, LoginModel.password)); }}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -