开发者

Difference in techniques for setting a stubbed method's return value with Rhino Mocks

开发者 https://www.devze.com 2023-01-01 06:17 出处:网络
What is the main difference between these following two ways to give a method some fake implementation?

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"


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

精彩评论

暂无评论...
验证码 换一张
取 消