2 Stimmen

Wie man einen Test für Konten Controller für Formulare authentifizieren schreiben

Ich versuche herauszufinden, wie ich meine Kontensteuerung angemessen testen kann. Ich habe Probleme beim Testen des erfolgreichen Anmeldeszenarios.

Problem 1) Fehlen mir noch andere Tests (ich teste die Attribute der Modellvalidierung separat)?

Problem 2) Put_ReturnsOverviewRedirectToRouteResultIfLogonSuccessAndNoReturnUrlGiven() und Put_ReturnsRedirectResultIfLogonSuccessAndReturnUrlGiven() Test sind nicht bestanden. Ich habe es auf die Zeile eingegrenzt, in der ich _membership.validateuser() aufrufe. Obwohl ich bei der Mock-Einrichtung des Dienstes angegeben habe, dass ich true zurückgeben möchte, wenn validateuser aufgerufen wird, gibt der Methodenaufruf false zurück.

Hier ist, was ich bis jetzt erhalten habe

AccountController.cs

[HandleError]
public class AccountController : Controller
{
    private IMembershipService _membershipService;

    public AccountController()
        : this(null)
    {
    }

    public AccountController(IMembershipService membershipService)
    {
        _membershipService = membershipService ?? new AccountMembershipService();
    }

    [HttpGet]
    public ActionResult LogOn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogOn(LogOnModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (_membershipService.ValidateUser(model.UserName,model.Password))
            {
                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Index", "Overview");
            }
            ModelState.AddModelError("*", "The user name or password provided is incorrect.");
        }

        return View(model);
    }

}

AccountServices.cs

public interface IMembershipService
{
    bool ValidateUser(string userName, string password);
}

public class AccountMembershipService : IMembershipService
{
    public bool ValidateUser(string userName, string password)
    {
        throw new System.NotImplementedException();
    }
}

AccountControllerFacts.cs

public class AccountControllerFacts
{
    public static AccountController GetAccountControllerForLogonSuccess()
    {
        var membershipServiceStub = MockRepository.GenerateStub<IMembershipService>();
        var controller = new AccountController(membershipServiceStub);
        membershipServiceStub
               .Stub(x => x.ValidateUser("someuser", "somepass"))
               .Return(true);
        return controller;
    }

    public static AccountController GetAccountControllerForLogonFailure()
    {
        var membershipServiceStub = MockRepository.GenerateStub<IMembershipService>();
        var controller = new AccountController(membershipServiceStub);
        membershipServiceStub
               .Stub(x => x.ValidateUser("someuser", "somepass"))
               .Return(false);
        return controller;
    }

    public class LogOn
    {
        [Fact]
        public void Get_ReturnsViewResultWithDefaultViewName()
        {
            // Arrange
            var controller = GetAccountControllerForLogonSuccess();

            // Act
            var result = controller.LogOn();

            // Assert
            Assert.IsType<ViewResult>(result);
            Assert.Empty(((ViewResult)result).ViewName);
        }

        [Fact]
        public void Put_ReturnsOverviewRedirectToRouteResultIfLogonSuccessAndNoReturnUrlGiven()
        {
            // Arrange
            var controller = GetAccountControllerForLogonSuccess();
            var user = new LogOnModel();

            // Act
            var result = controller.LogOn(user, null);
            var redirectresult = (RedirectToRouteResult) result;

            // Assert
            Assert.IsType<RedirectToRouteResult>(result);
            Assert.Equal("Overview", redirectresult.RouteValues["controller"]);
            Assert.Equal("Index", redirectresult.RouteValues["action"]);
        }

        [Fact]
        public void Put_ReturnsRedirectResultIfLogonSuccessAndReturnUrlGiven()
        {
            // Arrange
            var controller = GetAccountControllerForLogonSuccess();
            var user = new LogOnModel();

            // Act
            var result = controller.LogOn(user, "someurl");
            var redirectResult = (RedirectResult) result;

            // Assert
            Assert.IsType<RedirectResult>(result);
            Assert.Equal("someurl", redirectResult.Url);
        }

        [Fact]
        public void Put_ReturnsViewIfInvalidModelState()
        {
            // Arrange
            var controller = GetAccountControllerForLogonFailure();
            var user = new LogOnModel();
            controller.ModelState.AddModelError("*","Invalid model state.");

            // Act
            var result = controller.LogOn(user, "someurl");
            var viewResult = (ViewResult) result;

            // Assert
            Assert.IsType<ViewResult>(result);
            Assert.Empty(viewResult.ViewName);
            Assert.Same(user,viewResult.ViewData.Model);
        }

        [Fact]
        public void Put_ReturnsViewIfLogonFailed()
        {
            // Arrange

            var controller = GetAccountControllerForLogonFailure();
            var user = new LogOnModel();

            // Act
            var result = controller.LogOn(user, "someurl");
            var viewResult = (ViewResult) result;

            // Assert
            Assert.IsType<ViewResult>(result);
            Assert.Empty(viewResult.ViewName);
            Assert.Same(user,viewResult.ViewData.Model);
            Assert.Equal(false,viewResult.ViewData.ModelState.IsValid);
        }
    }
}

2voto

Anil Ali Punkte 478

Ich habe herausgefunden, wie ich meine Tests verbessern kann.

        [Fact]
        public void Put_ReturnsRedirectToRouteResultForOverviewIfLogonSuccessAndNoReturnUrlGiven()
        {
            // Arrange
            var mocks = new MockRepository();
            var mockMembershipService = mocks.StrictMock<IMembershipService>();
            using (mocks.Record())
            {
                Expect.Call(mockMembershipService.ValidateUser("", "")).IgnoreArguments().Return(true).Repeat.Any();
            }
            var controller = new AccountController(mockMembershipService);
            var user = new LogOnModel();

            // Act
            ActionResult result;
            using (mocks.Playback()){
                result = controller.LogOn(user, null);
            }

        // Assert
            Assert.IsType<RedirectToRouteResult>(result);
            var redirectresult = (RedirectToRouteResult)result;
            Assert.Equal("Overview", redirectresult.RouteValues["controller"]);
            Assert.Equal("Index", redirectresult.RouteValues["action"]);
        }

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X