文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
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.
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载