【发布时间】:2013-01-10 16:13:17
【问题描述】:
我正在尝试在类中模拟一个实例。这是类(简化):
public void CreatePhotos(string elementType)
{
var foo = new PicturesCreation();
//some code here...
if (elementType == "IO")
{
foo.CreateIcons(client, new PicturesOfFrogsCreation(), periodFrom, periodTo)
}
}
所以我试图模拟这个“new PicturesOfFrogsCreation()”来进行单元测试,看看是否使用这个参数调用了 CreateIcons。我试图在我的测试中使用 Rhino Mocks/AssertWasCalled 方法来实现这一点,但它看起来不起作用,因为我只知道如何模拟接口。你知道是否可以模拟这个吗?
更新:PictureCreation 类的代码:
internal sealed class PicturesCreation
{
public void CreateIcons(IPictures foo, int periodFrom, int periodTo)
{
foo.CreateIcons(periodFrom, periodTo);
}
}
以及PicturesOfFrogsCreation的代码:
internal sealed class PicturesOfFrogsCreation : IPictures
{
public void CreateIcons(int periodFrom, int periodTo)
{
//Code that implements the method
}
}
我写了这个测试,但我不确定它是否写得好:
public void Create_commitment_transaction_should_be_called_for_internal_order()
{
IPicture fooStub = RhinoStubWrapper<IPicture>.CreateStubObject();
rebuildCommitmentsStub.StubMethod(x => x.CreateIcons(Arg<int>.Is.Anything, Arg<int>.Is.Anything));
PicturesProcess.CreatePhotos("IO");
rebuildCommitmentsStub.AssertWasCalled(x => x.CreateIcons(Arg<int>.Is.Anything,Arg<int>.Is.Anything));
}
提前致谢!
一个。
【问题讨论】:
-
提取接口并使用它而不是具体类型有什么问题?
-
尝试从安排 -> 行动 -> 断言的角度思考。嘲笑是你安排的一部分。通常,您将“模拟”创建您验证的事物的事物。例如,您可以模拟存储库并描述返回“事物”的行为(即模拟您的 PicturesRepository 并告诉它在要求 pictureId == 42 时返回特定图片)。
-
@sll :问题是'//这里的一些代码'有很多情况,这是我不应该接触的代码。如果它很重要,我可以这样做,但这是一个小错误。
标签: c# rhino-mocks