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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:43:22+00:00 2026-05-15T02:43:22+00:00

What is the main difference between these following two ways to give a method

  • 0

What is the main difference between these following two ways to give a method some fake implementation?

I was using the second way fine in one test but in another test the behaviour can not be achieved unless I go with the first way.

so (the first),

using (test.Record()) //test is MockRepository instance
{
 service.GetUser("dummyName");
 LastCall.Return(new LoginUser());
}

vs (the second).

service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser());

Edit

The problem is that the second technique returns null in the test, when I expect it to return a new LoginUser. The first technique behaves as expected by returning a new LoginUser. All other test code used in both cases is identical.

[TestFixture]
public class AuthorizationTest
{
    private MockRepository test;
    private IMembershipService service;

    [SetUp]
    public void SetUp()
    {
        test = new MockRepository();
        service = test.Stub<IMembershipService>();

        using (test.Record())
        {
            service.GetUser("dummyName");
            LastCall.Return(new LoginUser());
        }

        //service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
    }

    [Test]
    public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
    {
        var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);

        LoginUser u = authStub.GetCurrentUser();
        Assert.That(u != null);
    }

    [TearDown]
    public void TearDown()
    {
        service = null;
        test = null;
    }
}

Could it be something to do with the overload in the interface perhaps?

public interface IMembershipService
{
    bool ChangePassword(string username, string oldPassword, string newPassword);
    LoginUser GetUser(string username);
    LoginUser GetUser(object providerUserKey);
    string ResetPassword(string username);
    bool ValidateUser(string username, string password);
}

The line causing the problem in the method under test is:

LoginUser currentUser = _repository.GetUser(identity.Name);

In debug mode, identity.Name is never null and is always “dummyName”

  • 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-15T02:43:23+00:00Added an answer on May 15, 2026 at 2:43 am

    the second method is the new AAA syntax, and is the preferred method. Perhaps you could expand on the problems you had using the AAA syntax, and you might be able to get help fixing that issue.

    EDIT:

    Are you sure that the method is being called with that parameter? What happens if you use

     service.Stub(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());
    

    do you get anything returned?

    What if you set an expectation rather than just stubbing it and then verify the expectation, might that give you some more information?

    service.Expect(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());
    service.VerifyAllExpectations();
    

    EDIT2:

    ok I did a bit of testing and it seems that it is the way you are creating your stub object. I assume that the way you are using is for the old style and that you need to use this for the new style:

    service = MockRepository.GenerateStub<IMembershipService>();
    

    When I changed to this I can run the tests and get them to pass. I’m not certain of the exact difference between using the static method to generate the stub and using the test MockRepository instance. Maybe someone else can explain the ins and outs of it.

    Anyway for completeness here is the code I ran which worked:

    [TestFixture]
    public class AuthorizationTest
        {
        private IMembershipService service;
    
        [SetUp]
        public void SetUp()
            {
            service = MockRepository.GenerateStub<IMembershipService>();
            service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
            }
    
        [Test]
        public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
            {
            var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);
    
            LoginUser u = authStub.GetCurrentUser();
            Assert.That(u != null);
            }
    
        [TearDown]
        public void TearDown()
            {
            service = null;
            }
        }
    
    public class AuthorizationStub_SetCurrentUserAuthenticated
        {
        private readonly IMembershipService m_service;
        private readonly bool m_b;
    
        public AuthorizationStub_SetCurrentUserAuthenticated (IMembershipService service, bool b)
            {
            m_service = service;
            m_b = b;
            }
    
        public LoginUser GetCurrentUser ()
            {
            return m_service.GetUser ("dummyName");
            }
        }
    
    public interface IMembershipService
        {
        bool ChangePassword(string username, string oldPassword, string newPassword);
        LoginUser GetUser(string username);
        LoginUser GetUser(object providerUserKey);
        string ResetPassword(string username);
        bool ValidateUser(string username, string password);
        }
    
    public class LoginUser
        {
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What are the main diffrences between these two implementations of LDAP protocol? Which is
The following string of mine tried to find difference between two strings. But it's
I came across a difference in speed using the following two structs: public struct
What are the main differences between these WPF controls? And when I should use
What is main difference between INSERT INTO table VALUES .. and INSERT INTO table
What is the main difference between Dispatcher view and Service to work design pattern?
What is the main difference between setInterval and setTimeout in JavaScript?
I want to know the main difference between .live() vs. .bind() methods in jQuery.
Please check below screen shot. What is main difference between Master Page and MVC
Possible Duplicate: Difference between void main and int main? Why is void main() {

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.