Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 702979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:46:50+00:00 2026-05-14T03:46:50+00:00

Trying to figure out how to adequately test my accounts controller. I am having

  • 0

Trying to figure out how to adequately test my accounts controller. I am having problem testing the successful logon scenario.

Issue 1) Am I missing any other tests.(I am testing the model validation attributes separately)

Issue 2) Put_ReturnsOverviewRedirectToRouteResultIfLogonSuccessAndNoReturnUrlGiven() and Put_ReturnsRedirectResultIfLogonSuccessAndReturnUrlGiven() test are not passing. I have narrowed it down to the line where i am calling _membership.validateuser(). Even though during my mock setup of the service i am stating that i want to return true whenever validateuser is called, the method call returns false.

Here is what I have gotten so far

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);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:46:50+00:00Added an answer on May 14, 2026 at 3:46 am

    Figured out how to fix my tests.

            [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"]);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

been trying to figure out Android layouts and having a bit of an issue.
Ok, so I'm having a problem trying figure out the problem in my code.
trying to figure out the best ActiveRecord inheritance strategy for this particular problem: I
Trying to figure out how to best handle the following scenario: Assume a RequestContext
I have a problem and Google hasn't helped me much. I'm trying figure out
Trying to figure out a rather annoying IE7 CSS issue. For some strange reason
Trying to figure out if it's possible and difficultly level around the below problem
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
Trying to figure out why my silverlight app suddenly just displays nothing (right click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.