views:

114

answers:

1

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"

+3  A: 

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
    {
    }
Sam Holder
Ok, thanks. I have added some more information.
CRice
updated my answer with some other ideas...
Sam Holder
Thanks for the examples. Its weird, the test fails still unless there is the presence of the stub with record and lastcall. When that stub is in there AND I also include your service.Expect and service.VerifyAllExpectations it passes. If i swap the record and lastcall stub with your stub example it fails.
CRice
I posted my interface, maybe the problem is due to the overload and the stub picks the wrong method?
CRice
No, renamed one of the methods and the problem is still there.
CRice
ok, think I found your issue. I have updated my answer.
Sam Holder
Thanks for your help, and for your efforts im going to give you something decent... +125 (in 24hrs). Still wonder why the instance method makes that much difference but anyway...
CRice