什么是模拟录制和回放?

发布于 2024-09-08 23:23:32 字数 446 浏览 5 评论 0原文

我有一个模拟如下:

MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();

我的问题是我已经看到上面的内容被用于 using 语句,例如

using (mocks.Record()) { // code here }
using (mocks.Playback()) { // code here }

这样做的目的是什么以及与我所做的有何不同?

I have a mock as below:

MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();

My question is I have seen the above being used in using statements e.g

using (mocks.Record()) { // code here }
using (mocks.Playback()) { // code here }

What is the purpose of this and difference to what I've done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-09-15 23:23:32

这些只是做同样事情的另一种语法。以下是等效的:

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
}

为了使事情变得更加复杂,有一种新的第三种语法,您不需要明确的记录和播放阶段,称为排列、动作、断言语法,请参见例如 http://ayende.com/blog/archive/2008/ 05/16/rhino-mocks--arrange-act-assert-syntax.aspx

These are just another syntax to do the same thing. The following are equivalent:

MockRepository mocks = new MockRepository();
ILoanRepository loanRepo = mocks.StrictMock<ILoanRepository>();
SetupResult.For(loanRepo.GetLoanExtended("sdfsdf")).Return(list.AsEnumerable<Loan>());
mocks.ReplayAll();
//test execution

and:

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
}

To make things even more complicated there is a new, third syntax where you don't have explicit record and playback phases called Arrange, Act, Assert Syntax, see e.g. http://ayende.com/blog/archive/2008/05/16/rhino-mocks--arrange-act-assert-syntax.aspx

橘和柠 2024-09-15 23:23:32

Record 块用于记录期望,因此 ReplayAll 之前的内容。

Playback 块实际上是在调用测试,所以 ReplayAll 之后会发生什么。

您可以在这里阅读更多相关信息:链接文本

The Record block is used to record expectations, so what comes before the ReplayAll.

The Playback block is actually calling the test, so what comes after the ReplayAll.

You can read more about it here: link text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文