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 8144541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:24:56+00:00 2026-06-06T13:24:56+00:00

I have a test where I’m mocking 2 service calls that get called in

  • 0

I have a test where I’m mocking 2 service calls that get called in a controller. The return value of the first call (a User POCO) is used to call the second. The problem is that it doesn’t seem to be matching the second because it keeps returning null. If I change the service to return a string instead of the User object, it works fine.

Test:

        [Test]
    public void GivenExternalReturnUrl_ReturnsHomePage()
    {
        var model = new Login
        {
            Username = "user1",
            Password = "password1",
            ReturnUrl = "http://google.com"
        };
        UserService.Setup(x => x.ValidateUser(model.Username, model.Password)).Returns(User);
        var cookie = new HttpCookie("test");
        UserService.Setup(x => x.BuildLoginCookie(User, model.RememberMe)).Returns(cookie);

        var result = Controller.Login(model) as RedirectToRouteResult;
        result.Should().Not.Be.Null();
        result.RouteValues["controller"].Should().Equal("Home");
        result.RouteValues["action"].Should().Equal("Index");
        UserService.Verify(x => x.BuildLoginCookie(User, model.RememberMe), Times.Once());
    }

BuildLoginCookie is returning null instead of the cookie object.

Action that it’s testing:

        [HttpPost]
    public ActionResult Login(Login model)
    {
        if (ModelState.IsValid)
        {
            var user = _userService.ValidateUser(model.Username, model.Password);
            if (user != null)
            {
                var cookie = _userService.BuildLoginCookie(user, model.RememberMe);
                Response.Cookies.Add(cookie);

                if (Url.IsLocalUrl(model.ReturnUrl))
                    return Redirect(model.ReturnUrl);
                return RedirectToAction("Index", "Home");
            }
            ModelState.AddModelError("InvalidCredentials", "The user name or password provided is incorrect.");
        }

        // If we got this far, something failed, redisplay form
        model.Password = null;  //clear the password so they have to re-enter it
        return View(model);
    }

What do I need to do so that Moq will match that second call and return the value I’m telling it to return?

  • 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-06T13:24:59+00:00Added an answer on June 6, 2026 at 1:24 pm

    The problem is with your property public UserWithRolesPermissions User { get { new UserWithRolesPermissions(); } }

    Every time you call it, it creates a new instance, hence the two references to it are each receiving a new instance, which are not equal. Therefore the calls to setup will not match the invocations.

    If you changed your code to use a single instance, it should work fine.

    var user = new UserWithRolesPermissions();
    UserService.Setup(x => x.ValidateUser(model.Username, model.Password)).Returns(user);
    var cookie = new HttpCookie("test");
    UserService.Setup(x => x.BuildLoginCookie(user, model.RememberMe)).Returns(cookie);
    

    Another option would be to avoid doing comparison of the instances, and just match on a property.

    UserService.Setup(x => x.BuildLoginCookie(It.Is<User>(u => u.Username == model.Username), model.RememberMe)).Returns(cookie);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Test controller and Test agent installed on a virtual machine VM1. Test
I have a Test Class, that test the access of all page with different
I have a test project, with an App.config that sets up default values for
I have test that runs in mstest, everything works fine and it gives me
If we have: test dword ptr [eax], 2000h je label1: Is there any value
How I update x.test to have values return from inheritance ? So I want
I've tryed mylyn but i cant find that feature, if anyone have test mantis
I have test input string (key=value&key=value...) and key (like this D2335A9CA4924D9F914D2B47B450D436) I need to
I have test code that I want to have a couple of hostnames resolve
I have test results in a log file that are formatted like: useless info

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.