BDD – MVC – Account Controller using Dependency Injection/ MSpec/ Moq – part 1





After completing my installation I started playing with MSpec.
I googled to see how people are using MSpec and what I can get from MSpec for my project.
My First Test was as following:

    [Subject(typeof(AccountController)), Tags("Login")]
    public class When_Logging_Into_the_application_with_an_invalid_user_name_or_password : AccountControllerContext
    {
        //Arrange
        private static ViewResult _result;
        private static LoginModel _model;
        private static string _loginFailureMessage = Autoprotect.Localization.Account.ResourceAccount.LoginFailureMessage;
        Establish context = () =>
        {
            _model = new LoginModel { UserName = "myuser", Password = "password@1", RememberMe = false };
        };
 
        //Act
        Because User_Attempts_To_Logs_In = () =>
        {
            _result = _controller.Login(_model, null) as ViewResult;
 
        };
 
        //Assert
        It should_return_the_logon_page = () =>
        {
            _result.ViewName.ShouldBeEmpty();
        };
 
        It Should_not_authenticate_the_user_into_the_application = () =>
        {
            _result.ViewData.Values.ShouldContain(_loginFailureMessage);
 
        };
 
        It Should_Not_Return_Invalid_Model_State = () =>
        {
            _controller.ModelState.IsValid.ShouldBeTrue();
        };
    }

I will explain later how i wrote this test and what does “AccountControllerContext” class is for




Tags: , , ,

One Response to “BDD – MVC – Account Controller using Dependency Injection/ MSpec/ Moq – part 1”

  1. Wonderful web site. Lots of helpful information here. I am sending it to some buddies ans also sharing in delicious. And naturally, thank you on your effort!