当使用嘲讽与伪造的C#单元测试? [英] When to use mocking versus faking in C# unit testing?

查看:190
本文介绍了当使用嘲讽与伪造的C#单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以拿出指导建议理想的情况下,选择嘲讽与伪造,即手动设置的要领?

Can anyone come up with guidelines suggesting the ideal scenarios to choose mocking versus faking, i.e., setting up the essentials manually?

我有点混淆如何处理这种情况。

I am a bit confused with how to approach this situation.

推荐答案

嗯,你有你需要理清一些事情。你有两个基本的东西,你需要知道:术语和最佳实践

Well you have a few things you need to sort out. You have two basic things you'll need to know: Nomenclature and Best Practices.

首先,我想给你一个巨大的测试一个伟大的视频资源,罗伊Osherove:

First I want to give you a great video resource from a great tester, Roy Osherove:

单元测试评价时,罗伊Osherove

他说,他已经开始了   完成测试工具的一些评论   随几个开源   项目。你可以在这里找到这些:   <一href="http://weblogs.asp.net/rosherove/archive/tags/TestReview/default.aspx">http://weblogs.asp.net/rosherove/archive/tags/TestReview/default.aspx

He starts out by saying that he has done some reviews of test harnesses shipped with several open source projects. You can find those here: http://weblogs.asp.net/rosherove/archive/tags/TestReview/default.aspx

这些都是基本的视频评论   在那里,他将引导您完成这些测试   线束,告诉你什么是好   什么是坏。非常有帮助。

These are basically video reviews where he walks you through these test harnesses and tells you what is good and what is bad. Very helpful.

罗伊也有一本书,我明白了   是非常不错的。

Roy also has a book that I understand is very good.

命名

本播客将助阵   非常的<一个href="http://www.hanselminutes.com/default.aspx?showID=187">http://www.hanselminutes.com/default.aspx?showID=187

我会套用播客,虽然   (即Hanselminutes介绍音乐   可怕的):

I'll paraphrase the podcast, though (that Hanselminutes intro music is dreadful):

基本上,你所做的一切与   隔离框架的(如最小起订量,犀牛制品,模拟型等)被称为   的。

Basically everything you do with an isolation framework (like Moq, Rhino Mocks, Type Mock, etc) is called a fake.

A 是在使用一个对象时   一个测试,在code,你正在测试   可以代替生产code调用。   一个假被用来隔离code您   试图从其他地方来测试   您的应用程序。

A fake is an object in use during a test that the code you are testing can call in place of production code. A fake is used to isolate the code you are trying to test from other parts of your application.

有(为主)两种假货存根的   和嘲笑的。

A 模拟那你把是假的   到位,使code,你正在测试   能叫出它,你断言   此次呼叫使用正确的   参数。下面的示例不只是   这种使用起订量隔离   框架:

A mock is a fake that you put in place so that the code you are testing can call out to it and you assert that the call was made with the correct parameters. The below sample does just this using the Moq isolation framework:

[TestMethod]
public void CalculateTax_ValidTaxRate_DALCallIsCorrect()
{
    //Arrange
    Mock<ITaxRateDataAccess> taxDALMock = new Mock<ITaxRateDataAccess>();
    taxDALMock.Setup(taxDAL => taxDAL.GetTaxRateForZipCode("75001"))
                  .Returns(0.08).Verifiable();

    TaxCalculator calc = new TaxCalculator(taxDALMock.Object);

    //Act
    decimal result = calc.CalculateTax("75001", 100.00);

    //Assert
    taxDALMock.VerifyAll();
}

     

A 存根几乎是一样的   模拟,但你把它在的地方   以确保code你是测试   会从后面的数据相一致的   调用(比如,如果你的code电话   一个数据访问层,一个存根会   返回假数据),但你不知道   主张对存根本身。那   是,你不小心验证   方法中调用你的假数据访问   层 - 你正在试图测试   别的东西。您提供的存根   让你尝试的方法   测试孤立地工作。

A stub is almost the same as a mock, except that you put it in place to make sure the code you are testing gets back consistent data from its call (for instance, if your code calls a data access layer, a stub would return back fake data), but you don’t assert against the stub itself. That is, you don’t care to verify that the method called your fake data access layer – you are trying to test something else. You provide the stub to get the method you are trying to test to work in isolation.

下面是一个存根的例子:

Here’s an example with a stub:

[TestMethod]
public void CalculateTax_ValidTaxRate_TaxValueIsCorrect()
{    
    //Arrange
    Mock<ITaxRateDataAccess> taxDALStub = new Mock<ITaxRateDataAccess>();
    taxDALStub.Setup(taxDAL => taxDAL.GetTaxRateForZipCode("75001"))
                  .Returns(0.08);

    TaxCalculator calc = new TaxCalculator(taxDALStub.Object); 

    //Act
    decimal result = calc.CalculateTax("75001", 100.00);

    //Assert
    Assert.AreEqual(result, 8.00);
}

     

在这里请注意,我们正在测试的   输出的方法的,而不是   事实的方法制作的调用   其他资源。

Notice here that we are testing the output of the method, rather than the fact that the method made a call to another resource.

起订量并没有真正的API   一个模拟和存根之间的区别   (请注意这两个被宣布为   模拟&LT; T&GT; ),但用法这里   重要在确定类型

Moq doesn’t really make an API distinction between a mock and a stub (notice both were declared as Mock<T>), but the usage here is important in determining the type.

希望这有助于直接设置你。

Hope this helps set you straight.

这篇关于当使用嘲讽与伪造的C#单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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