Rhino Mocks 中的 RR 模式 与 AAA 模式
时间:2011-04-13 来源:szh114
RR(Record Replay) 模式示例:
// Prepare mock repository
MockRepository mocks = new MockRepository();
IDependency dependency = mocks.CreateMock<IDependency>();
// Record expectations
using ( mocks.Record() )
{
Expect
.Call( dependency.SomeMethod() )
.Return( null );
}
// Replay and validate interaction
object result;
using ( mocks.Playback() )
{
IComponent underTest = new ComponentImplementation( dependency );
result = underTest.TestMethod();
}
// Post-interaction assertions
Assert.IsNull( result );
This is a simplified example; as you can see, the expectations are recorded in one scope which also takes care (courtesy of the using keyword) of calling mocks.ReplayAll(). The interaction testing itself takes place in another scope, after which (again via using) a call is made to mocks.VerifyAll().
AAA(Arrange,Act,Assert)模式示例:
[Test]
public void AAA()
{
// arrange
var view = MockRepository.GenerateMock<IView>();
new Presenter(view); // subscribe to the event
// act
view.GetEventRaiser(v => v.Foo += null).Raise("5");
// assert
view.AssertWasCalled(v => v.SetResult(Arg<double>.Is.Equal(5d)));
}
- When using the Arg<T> constraint, make sure the type passed to Equal() is exactly right. Use Arg<double>.Is.Equal(5d)) and not Arg<double>.Is.Equal(5)).
- In both versions, the Raise() method is not strongly typed.
相关阅读 更多 +