我有一个模拟如下:
MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();
我的问题是我已经看到上面用于使用语句,例如
using (mocks.Record()) { // code here }
using (mocks.Playback()) { // code here }
这个的目的是什么,与我所做的不同之处是什么?
答案 0 :(得分:1)
答案 1 :(得分:1)
这些只是做同样事情的另一种语法。以下是等效的:
MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();
//test execution
和
MockRepository mocks = new MockRepository();
using (mocks.Record()) {
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
}
using (mocks.Playback()) {
//test execution
}
为了使事情变得更复杂,有一种新的第三种语法,你没有明确的记录和回放阶段,称为Arrange,Act,Assert Syntax,参见例如http://ayende.com/blog/archive/2008/05/16/rhino-mocks--arrange-act-assert-syntax.aspx