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

  • SEARCH
  • Home
  • 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 8268279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:49:39+00:00 2026-06-08T05:49:39+00:00

I have a problem testing a logon method using existing AccountController (made by MVC)…

  • 0

I have a problem testing a logon method using existing AccountController (made by MVC)…

I have this simple test method:

  [TestMethod]
  public void LogOnTest1() {
     AccountController controller = new AccountController();

     LogOnModel logonModel = new LogOnModel();
     logonModel.UserName = "test";
     logonModel.Password = "test1234";

     if ( controller.MembershipService == null ) {
        controller.MembershipService = new AccountMembershipService();
     }

     if ( controller.FormsService == null ) {
        controller.FormsService = new FormsAuthenticationService();
     }

     var result = controller.LogOn( logonModel, "" ) as ViewResult;

     Assert.AreEqual( "Index", result.ViewName );
  }

and the method defined in AccountController:

[HttpPost]
      public ActionResult LogOn( LogOnModel model, string returnUrl ) {
         if ( ModelState.IsValid ) {
            if ( MembershipService.ValidateUser( model.UserName, model.Password ) ) {
               FormsService.SignIn( model.UserName, model.RememberMe );
               if ( !string.IsNullOrEmpty( returnUrl ) ) {
                  return Redirect( returnUrl );
               } else {
                  return RedirectToAction( "Index", "Home" );
               }
            } else {
               ModelState.AddModelError( "", "The user name or password provided is incorrect." );
            }
         }

         // If we got this far, something failed, redisplay form
         return View( model );
      }

The above method is not defined/modified by me. Just created by when create an asp.net mvc project.

The problem is at line

if ( MembershipService.ValidateUser( model.UserName,model.Password ) ) {

which returns always false though I provided correct login info.

Where is my mistake ?

  • 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-06-08T05:49:42+00:00Added an answer on June 8, 2026 at 5:49 am

    First of all, do not use if statements and other conditional logic in your tests. Also do not use concrete classes in your tests. If this test will fail, how will you know why? What was broken – controller or AccountMembershipService? Use abstract dependencies, which could be mocked.

    public class AccountController : Controller
    {
        private IMembershipService _membershipService;
        private IFormsService _formsService;
    
        public AccountController(IMembershipService membershipService,
                                 IFormsService formsService)
        {
            _membershipService = membershipService;
            _formsService = formsService;
        }
    
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            // implementation goes here
        }
    }
    

    And your tests:

    [Test]
    public void ShouldNotAcceptInvalidUser()
    {
        // Arrange
        Mock<IMembershipService> membership = new Mock<IMembershipService>();
        membership.Setup(m => m.ValidateUser(It.IsAny<string>(), It.IsAny<string>()))
                  .Returns(false);
        Mock<IFormsService> forms = new Mock<IFormsService>();
        var logonModel = new LogOnModel() { UserName = "", Password = "" };
        var controller = new AccountController(membership.Object, forms.Object);
    
        // Act
        var result = controller.LogOn(logonModel, "") as ViewResult;
    
        // Assert
        Assert.That(result.ViewName, Is.EqualTo("Index"));
        Assert.False(controller.ModelState.IsValid);
        Assert.That(controller.ModelState[""], 
                    Is.EqualTo("The user name or password provided is incorrect."));
    }
    

    Interesting here is that you don’t care which logon info you passed to controller. You mock response from membership service, which says logon was incorrect.

    FEW TESTS MORE:

    [Test]
    public void ShouldRedisplayViewWhenModelIsNotValid()
    {
       // Arrange        
       Mock<IMembershipService> membership = new Mock<IMembershipService>();
       Mock<IFormsService> forms = new Mock<IFormsService>();
       var model = new LogOnModel() { UserName = "", Password = "" };
       var controller = new AccountController(membership.Object, forms.Object);
       controller.ModelState.AddModelError("key", "error message");
       // Act
       var result = controller.LogOn(model, "") as ViewResult;
       // Assert
       Assert.That(result.ViewName, Is.EqualTo("LogOn"));
    }
    
    [Test]
    public void ShouldSignInAndRedirectToIndex()
    {
       // Arrange        
       Mock<IMembershipService> membership = new Mock<IMembershipService>();
      membership.Setup(m => m.ValidateUser(It.IsAny<string>(), It.IsAny<string>()))
                 .Returns(true);
       Mock<IFormsService> forms = new Mock<IFormsService>();
       var model = new LogOnModel() { UserName = "", Password = "" };
       var controller = new AccountController(membership.Object, forms.Object);
       controller.ModelState.AddModelError("key", "error message");
       // Act
       var result = controller.LogOn(model, "") as ViewResult;
    
       // Assert
       forms.Verify(f => f.SignIn(model.UserName, model.RememberMe));
       Assert.That(result.ViewName, Is.EqualTo("Index"));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem with WCF. My testing code is pretty simple. I call
I have a problem executing a process from our testing server. On my localhost
I'm testing a new TeeChart HTML5/javascript and I have a problem with zoom. When
I'm working on the n-queens problem and am testing what I have so far
I have a very strange problem when I'm testing my application on device. I
I have quite a strange problem with PHP and Apache on my local testing
Here a small piece of code for testing and explain the problem. I have
I'm testing the twitter API (with OAuth) and I have a little problem, I
I'm new to Drupal and am experiencing this strange problem with my testing Drupal

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.