为什么 ExpectConstructor() 不会导致此测试失败?

发布于 2024-07-15 22:38:06 字数 1274 浏览 6 评论 0原文

我对 TypeMock 3.5 中 Mock 类的 ExpectConstructor() 方法的用途有点困惑。

我本以为如果未调用构造函数,即如果未调用构造函数,则调用 ExpectConstructor 会导致 MockManagerVerify() 上失败模拟类型未实例化。

但是,调用 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 技术交流群。

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

发布评论

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

评论(1

完美的未来在梦里 2024-07-22 22:38:06

早在 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 :(

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