我正在尝试测试方法。 我想确保在该方法中有一个方法调用(调用服务) 代码看起来像这样:
using(proxy = new Proxy())
{
proxy.CallService();
}
我用假对象交换代理(使用TypeMock) 但是由于虚假对象在块的末尾处理而得到错误。 我不想删除那个“使用”块。 感谢
答案 0 :(得分:1)
使模拟对象期望也被处理掉。 (我不知道TypeMock,但在其他一些模拟框架中,这只是在“记录”阶段自己调用Dispose
的情况。)事实上,我会说没有它,测试是错误的无论如何,因为大概你想要确定代理 被处置。
答案 1 :(得分:0)
只是指示您的代理模拟器也期望调用Dispose()
。如果你正在使用反光模拟,你需要做这样的事情
var proxyMock = MockManager.Mock(typeof(Proxy));
// set up regular calls to proxy, e.g.
proxyMock.ExpectCall("CallService");
proxyMock.ExpectCall("Dispose");
对于自然模拟,在您的使用区块中调用Dispose()
。
答案 2 :(得分:0)
使假对象实现IDisposable:
class FakeObject : IDisposable
{
public void Dispose() {}
}
答案 3 :(得分:0)
免责声明:我在Typemock工作 如果您使用的是Arrange Act Assert API,则可以使用Members.ReturnRecursiveFakes 当您创建假对象时(注意:这是5.2.0版本的默认值) 这也会自动伪造Dispose方法。
所以你的测试将是这样的:
var fake = Isolate.Fake.Instance<Proxy>(Members.ReturnRecursiveFakes);
Isolate.WhenCalled(() => fake.CallService()).IgnoreCall();
Isolate.Swap.NextInstance<Proxy>().With(fake);
UnderTest classUnderTest = new ClassUnderTest();
classUnderTest.MethodUnderTest(); // assuming the Proxy instance is used here.
Isolate.Verify.WasCalledWithAnyArguments(()=>fake.CallService());
我想补充Jon Skeet所说的我会创建一个单独的测试来确保调用Dispose方法。
我认为在每种测试方法中断言一件事是一种好习惯,这样当测试分解时你就会立即知道原因。