文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Rhino Mocks 中的 RR 模式 与 AAA 模式

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.
相关阅读 更多 +
排行榜 更多 +
3d射击球球手机游戏

3d射击球球手机游戏

飞行射击 下载
王铲铲的运河梦折相思内置菜单

王铲铲的运河梦折相思内置菜单

休闲益智 下载
星座乐消消游戏

星座乐消消游戏

休闲益智 下载