为什么 ExpectConstructor() 不会导致此测试失败?
我对 TypeMock 3.5 中 Mock
类的 ExpectConstructor()
方法的用途有点困惑。
我本以为如果未调用构造函数,即如果未调用构造函数,则调用 ExpectConstructor
会导致 MockManager
在 Verify()
上失败模拟类型未实例化。
但是,调用 ExpectConstructor()
而不指定该构造函数的任何参数似乎不会设置该期望 - 因此我的测试无论如何都会通过。
我的问题:我是否遗漏或误解了某些内容? 如果 ExpectConstructor()
不是用于验证构造函数调用,那么它的用途是什么?
考虑这三个 NUnit 测试来说明问题:
[Test]
public void exampleTest1()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor().Args(10);
Foo f = new Foo(10); // Constructor called
MockManager.Verify();
// This test passes, as expected...so far so good
}
[Test]
public void exampleTest2()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor();
Foo f = new Foo(); // Constructor called
MockManager.Verify();
// This test passes...also as expected
}
[Test]
public void exampleTest3()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor();
// nb. not instantiating an instance of Foo
MockManager.Verify();
// This test passes - why?!
}
提前感谢您的帮助。
I'm a little confused as to the purpose of the ExpectConstructor()
method on the Mock
class in TypeMock 3.5.
I would have thought that calling ExpectConstructor
would cause the MockManager
to fail upon Verify()
if the constructor is not called, i.e. if an instance of the mocked type is not instantiated.
However, calling ExpectConstructor()
without specifying any arguments to that constructor doesn't appear to set that expectation - and so my test passes regardless.
My questions: Am I missing or misunderstanding something? If ExpectConstructor()
isn't for verifying a constructor call, what's it for?
Consider these three NUnit tests which illustrate the problem:
[Test]
public void exampleTest1()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor().Args(10);
Foo f = new Foo(10); // Constructor called
MockManager.Verify();
// This test passes, as expected...so far so good
}
[Test]
public void exampleTest2()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor();
Foo f = new Foo(); // Constructor called
MockManager.Verify();
// This test passes...also as expected
}
[Test]
public void exampleTest3()
{
MockManager.Init();
Mock fooMock = MockManager.Mock(typeof(Foo));
fooMock.ExpectConstructor();
// nb. not instantiating an instance of Foo
MockManager.Verify();
// This test passes - why?!
}
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
早在 2006 年就报告了这个错误。 据说它会被修复(好吧,已实现 - 显然这不是一个错误,它只是一个未实现的功能......)但如果现在仍然是一个问题,我猜他们没有解决这个问题:(
This was reported as a bug back in 2006. Supposedly it was going to be fixed (well, implemented - apparently it wasn't a bug, it was just an unimplemented feature...) but if it's still a problem now, I guess they didn't get round to it :(