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"