多输入单元测试 [英] Unit testing with multiple inputs
问题描述
我一直在试图环绕单元测试我的头,我试图处理单元测试的返回值取决于一堆参数的函数。有很多的信息,但是,这是一个有点势不可挡。
I've been trying to wrap my head around unit testing and I'm trying to deal with unit testing a function whose return value depends on a bunch of parameters. There's a lot of information however and it's a bit overwhelming..
考虑以下几点:
我有一个类文章
,其价格的集合。它有一个方法 GetCurrentPrice
它根据一些规则目前的价格:
I have a class Article
, which has a collection of prices. It has a method GetCurrentPrice
which determines the current price based on a few rules:
public class Article
{
public string Id { get; set; }
public string Description { get; set; }
public List<Price> Prices { get; set; }
public Article()
{
Prices = new List<Price>();
}
public Price GetCurrentPrice()
{
if (Prices == null)
return null;
return (
from
price in Prices
where
price.Active &&
DateTime.Now >= price.Start &&
DateTime.Now <= price.End
select price)
.OrderByDescending(p => p.Type)
.FirstOrDefault();
}
}
的
PriceType
枚举和价格
类:
public enum PriceType
{
Normal = 0,
Action = 1
}
public class Price
{
public string Id { get; set; }
public string Description { get; set; }
public decimal Amount { get; set; }
public PriceType Type { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool Active { get; set; }
}
我想创建为一个单元测试 GetCurrentPrice
方法。基本上,我想测试可能可能发生的所有规则的组合,所以我必须创建多篇文章,以遏制房价的各种组合来获得全覆盖。
I want to create a unit test for the GetCurrentPrice
method. Basically I want to test all combinations of rules that could possibly occur, so I would have to create multiple articles to contain various combinations of prices to get full coverage.
我单元测试m的思维像这样的(伪):
I'm thinking of a unit test such as this (pseudo):
[TestMethod()]
public void GetCurrentPriceTest()
{
var articles = getTestArticles();
foreach (var article in articles)
{
var price = article.GetCurrentPrice();
// somehow compare the gotten price to a predefined value
}
}
-
我读过,多个断言是邪恶的,但没有我需要
他们在这里测试的所有条件?或者,我需要每个条件一个独立的单元
测试?I've read that 'multiple asserts are evil', but don't I need them to test all conditions here? Or would I need a separate unit test per condition?
我怎么会去提供单元测试用一组测试数据?
我应该嘲笑一个资料库?并应这些数据还包括
的预期值?How would I go about providing the unit test with a set of test data? Should I mock a repository? And should that data also include the expected values?
推荐答案
你没有使用在这个例子中一个仓库,所以没有必要嘲笑什么。你可以做的就是为不同的可能的输入创建多个单元测试:
You are not using a repository in this example so there's no need to mock anything. What you could do is to create multiple unit tests for the different possible inputs:
[TestMethod] public void Foo() { // arrange var article = new Article(); // TODO: go ahead and populate the Prices collection with dummy data // act var actual = article.GetCurrentPrice(); // assert // TODO: assert on the actual price returned by the method // depending on what you put in the arrange phase you know }
等等,你可以添加其他的单元测试,你只会修改
安排每一个可能的输入
和断言
阶段。and so on you could add other unit tests where you would only change the
arrange
andassert
phases for each possible input.这篇关于多输入单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!