Rhino Mocks overwriting stubs possible?
I have this problem, which m开发者_Python百科ay be a bug in Rhino Mocks 3.5:
stubObj = MockRepository.GenerateStub(IObject);
stubObj.Stub(a=>a.Get()).Return (Guid.Empty); //1.stub
stubObj.Stub(a=>a.Get()).Return (Guid.NewGuid()); //2.stub, should overwrite the first one?
this:
var value = stubObj.Get();
returns Guid.Empty
, is this the correct behavior?
If you want to return the empty guid a known number of times, the only thing you have to do is to tell RhinoMocks how many times to repeat it, e.g. the following test passes:
[Test]
public void MultipleStubsTest()
{
var testMock = MockRepository.GenerateMock<ITest>();
testMock.Stub(x => x.Get()).Return(Guid.Empty).Repeat.Once();
testMock.Stub(x => x.Get()).Return(Guid.NewGuid());
Assert.AreEqual(Guid.Empty, testMock.Get());
Assert.AreNotEqual(Guid.Empty, testMock.Get());
}
if you don't know how many times Get() will be called before the guid should change, than you can always use .Do() and code it there (please let me know if you need more details).
You have just programmed the Stub object for two separate calls. If you call stubObj.Get again, you should get what Guid.NewGuid
generated. You can prepare your fake object for any number of invocations of different kinds. For this reason, it doesnt make sense to expect the last .Stub
call for a given invocation to replace previous .Stub
bings of that call.
In your test code, which should be short and neat, there should never be a case where you need to 'undo' such programming of the mock in the way you seem to want to do.
If what needs to be returned is a conditional thing which varies depending on other bits of your test code across multiple calls to this block of code, the last thing you want is magic happening to make readers have to figure out what you meant. If it's conditional, you should make it clear.
And then, when you've made it clear, refactor it out as you should not have Conditional Logic in Tests (see xUnit Test Patterns)
That's how you would do it. It passes on my machine.
[TestMethod]
public void Test()
{
stubObj = MockRepository.GenerateMock<IGuidTest>();
stubObj.Stub(a => a.Get()).Repeat.Times(1).Return(Guid.Empty);
stubObj.Stub(a => a.Get()).Repeat.Times(1).Return(Guid.NewGuid());
Assert.AreEqual(Guid.Empty, stubObj.Get());
Assert.AreNotEqual(Guid.Empty, stubObj.Get());
}
public IGuidTest stubObj;
public interface IGuidTest
{
Guid Get();
}
The following tests should pass:
// arrange
var stubObj = MockRepository.GenerateStub<IObject>();
stubObj.Stub(a => a.Get())
.Return(Guid.Empty);
// act
var value = stubObj.Get();
// assert
Assert.AreEqual(Guid.Empty, value);
And if you want to return a new guid:
// arrange
var stubObj = MockRepository.GenerateStub<IObject>();
stubObj.Stub(a => a.Get())
.Return(Guid.NewGuid());
// act
var value = stubObj.Get();
// assert
Assert.AreNotEqual(Guid.Empty, value);
You have the option to reset all expectations if that seems appropriate.
public interface IObject { Guid Get(); }
[Test]
public void OverwriteStubTest()
{
Guid ret = Guid.NewGuid();
var stub = MockRepository.GenerateMock<IObject>();
stub.Stub(a => a.Get()).Return(Guid.Empty);
Assert.AreEqual(Guid.Empty, stub.Get());
//Reset all expectations on this mock object
stub.BackToRecord();
stub.Stub(a => a.Get()).Return(ret);
stub.Replay();
Assert.AreEqual(ret, stub.Get());
}
Yes, this is the expected behavior. Writing the expectations/stubs in that way tells the mocking framework to return Guid.Empty the first time the Get() method is called and return Guid.NewGuid() the second time it is called.
Here are a few ways to "overwrite" the first expectation with the second: How to change behaviour of stubs?
精彩评论