gpt4 book ai didi

c# - 何时在 C# 单元测试中使用模拟与伪造?

转载 作者:IT王子 更新时间:2023-10-29 03:49:55 24 4
gpt4 key购买 nike

谁能提出指导方针,建议选择模拟与伪造的理想场景,即手动设置必需品?

我对如何处理这种情况有点困惑。

最佳答案

好吧,您需要解决一些问题。您需要了解两件基本的事情:术语和最佳实践。

首先,我想为您提供来自优秀测试员 Roy Osherove 的精彩视频资源:

Roy Osherove 的单元测试评论

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.

命名法

This podcast will help out immensely: http://www.hanselminutes.com/default.aspx?showID=187

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 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.

There are (mainly) two types of fakes: stubs and mocks.

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 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.

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.

希望这可以帮助您理清思路。

关于c# - 何时在 C# 单元测试中使用模拟与伪造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1419713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com