C#的嘲弄框架 - 简单,内容丰富异常 [英] C# Mocking Framework - simple with informative exceptions

查看:176
本文介绍了C#的嘲弄框架 - 简单,内容丰富异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在使用NUnit.Mocks隔离我的课,但我越来越恼火,缺乏反馈它给了我。所以,我一直在四处寻找替代品,但还是一无所获。

So far I've been using NUnit.Mocks to isolate my classes but I am getting annoyed by the lack of feedback it gives me. So I've been looking around for alternatives but getting nowhere.

犀牛制品:不能弄明白。一切都必须在一个完全不同的晦涩的方式进行使测试code几乎无法读取。

Rhino Mocks: Can't figure it out. Everything has to be done in a totally different obscure manner making the test code practically unreadable.

<打击> 起订量:无法弄清楚如何嘲笑财产干将。有样,但是当我尝试他们,他们不工作。

Moq: Can't figure out how to mock property getters. There are samples but they don't work when I try them.

从我收集的这些似乎是时下最流行的。在我去试试每一个人在那里,我想问问你的伙计建议...

From what I've gathered these seem to be among the most popular ones. Before I go and try every single one out there I'd like to ask you folks for suggestions...

  • 设计简单。我不想记住100种不同的报表,哪一个取决于我的方法是否有返回值的,我的财产是否具有两个getter和setter或只是其中的一个使用,无论它是一个满月与否等...
  • 精品反馈。如果我的测试失败,我想知道什么方法调用缺失或过多。我想知道哪些调用了错误的参数。等等...
  • Simple design. I don't want to memorize 100 different statements and which one to use depending on whether my method has a return value or not, whether my property has both getters and setters or just one of them, whether it's a full moon or not, etc...
  • Elaborate feedback. If my test fails I want to know what method call was missing or too much. I want to know which call had the wrong arguments. Etc...

我的背景是C#2.0。我仍然pretty的联合国熟悉新的.NET版本的概念。因此,一个框架,它并不需要这些东西将是一个奖金。

My background is C# 2.0. I'm still pretty UN-familiar with the concepts in newer .NET versions. So a framework that doesn't require those things would be a bonus.

感谢你。

编辑:我终于想通了,为什么我起订量测试,没有工作。它有没有关系起订量,我现在进一步评估它。看起来非常好,到目前为止...

I finally figured out why my Moq tests didn't work. It had nothing to do with Moq and I am now evaluating it further. Looks very good so far...

推荐答案

我用的起订量和I preFER实施,而不是其他一些嘲讽框架的重播方法 - 我发现它更自然

I use Moq and I prefer the implementation rather than the Replay method of some other mocking frameworks - I found it more natural.

您具有属性什么问题?为了模拟从属性,这只是返回一个值

What problem are you having with Properties? To mock returning a value from a property it is just

mock.Setup(foo => foo.Name).Returns("bar")

简单的设计

起订量有一个流畅的接口和code能恩pressive和相当漂亮真的。例如:

Moq has a fluent interface and the code can be expressive and quite beautiful really. For example:

  1. 注射嘲笑为对象

  1. Injecting mocks into object

mockView = new Mock<IView>();
mockModel = new Mock<IModel>();
realPresenter = new Presenter(mockView.Object, mockModel.Object);

  • 测试presenter行为 反应查看事件

  • Testing behavior of Presenter reacting to View event

    mockView.Raise(v => v.SomeEvent += null, EventArgs.Empty);
    mockModel.Verify(m => m.DoSomething());
    

  • 精品反馈

    我觉得失败的反馈非常有用,我经常能发现问题非常快。假设你期望调用某些参数值的方法,它失败。

    I find the failure feedback very useful and I can usually find the problem very quickly. Say you expect a method with certain parameter value to be called and it fails.

    测试MyProject.MyTest'失败:     Moq.MockException:   预计调用的模拟至少一次,但从未进行:v   =>> v.DoSomething(东西)>没有配置设置。

    Test 'MyProject.MyTest' failed: Moq.MockException : Expected invocation on the mock at least once, but was never performed: v => >v.DoSomething("Something")> No setups configured.

    执行的调用: View.DoSomething(SomethingElse)     在   Moq.Mock.ThrowVerifyException(MethodCall   预计,IEnumerable的 1的设置,   的IEnumerable 1 actualCalls,防爆pression   EX pression,倍,的Int32   callCount)在   Moq.Mock.VerifyCalls(拦截器   targetInterceptor,MethodCall   预计,防爆pression EX pression,时代   次)在Moq.Mock.Verify [T](模拟   模拟,防爆pression 1 EX pression,时代   次,串failMessage)在   Moq.Mock 1.Verify(出pression`1   EX pression)MyTest.cs(118,0):

    Performed invocations: View.DoSomething("SomethingElse") at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable1 setups, IEnumerable1 actualCalls, Expression expression, Times times, Int32 callCount) at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times) at Moq.Mock.Verify[T](Mock mock, Expression1 expression, Times times, String failMessage) at Moq.Mock1.Verify(Expression`1 expression) MyTest.cs(118,0):

    借助起订量快速启动是很好的放在一起,一直唯一参考我所需要的为止。

    The Moq Quickstart is very well put together and has been the only reference I have needed so far.

    这篇关于C#的嘲弄框架 - 简单,内容丰富异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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