0

我正在使用 RhinoMock 进行单元测试,我想知道如何设置它,以便特定方法始终返回它作为参数接收的相同对象。

这是我要模拟的界面:

public interface IItemRepository
{
    Item Craete(Item item);
}

我想以这样的方式设置 RhinoMocks,每次调用 Create 方法时,模拟的存根将返回作为参数传递的相同对象。

这是我的测试初始化​​方法:

[TestInitialize]
public void CrateServiceWithMockRepository()
{
    var stubRepository = MockRepository.GenerateStub<IItemRepository>();

    // ... how to set-up the stubRepository as described above ...

    // Create the target service for this unit test
    this.targetService = new ServiceXYZ(stubRepository);
}
4

1 回答 1

2

您可以Do在调用方法时使用并传递委托来执行:

stubRepository.Stub(r => r.Create(null)).IgnoreArguments()
    .Do(new Func<Item, Item>(item => item));
于 2013-05-02T17:27:22.143 回答