为什么我们需要嘲弄框架? [英] Why do we need mocking frameworks?

查看:131
本文介绍了为什么我们需要嘲弄框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与代码其中有NUnit测试工作撰写。但是,我从来没有与嘲讽的框架工作。他们是什么?我理解依赖注入以及它如何帮助提高可测性。我的意思是所有的依赖关系可以在单元测试被嘲笑。但是,那为什么我们需要嘲弄框架?我们不能简单地创建模拟对象,并提供相关性。我失去了一些东西在这里?
感谢。

I have worked with code which had NUnit test written. But, I have never worked with mocking frameworks. What are they? I understand dependency injection and how it helps to improve the testability. I mean all dependencies can be mocked while unit testing. But, then why do we need mocking frameworks? Can't we simply create mock objects and provide dependencies. Am I missing something here? Thanks.

推荐答案


  • 这使得嘲弄容易

  • 他们通常
    让你表达引用对象之间的
    交互检验的
    断言。

这里有一个例子:

var extension = MockRepository
    .GenerateMock<IContextExtension<StandardContext>>();
  var ctx = new StandardContext();
  ctx.AddExtension(extension);
  extension.AssertWasCalled(
    e=>e.Attach(null), 
    o=>o.Constraints(Is.Equal(ctx)));

您可以看到,我明确地测试该IContextExtension的连接方法被调用和输入参数有人说上下文对象。它将使如果这没有发生在我的测试失败。

You can see that I explicitly test that the Attach method of the IContextExtension was called and that the input parameter was said context object. It would make my test fail if that did not happen.

这篇关于为什么我们需要嘲弄框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆