【发布时间】:2012-06-12 13:51:54
【问题描述】:
我对单元测试、TDD 和 mocking 很陌生,但是我理解一般概念。我的问题是如何模拟一个类,以便我可以调用实例化的方法,而不会在我的单元测试和实现类中重复代码?鉴于以下情况:
//Simple Interface
public interface IProduct {
double price { get; set; }
double tax { get; set; }
double calculateCost();
}
//Simple Implementation of IProduct
public class SimpleProduct : IProduct {
private double _price;
private double _tax;
public double price {
get { return _price; }
set { _price = value; }
}
public double tax {
get { return _tax; }
set { _tax = value; }
}
public double calculateCost() {
return _price + (_price * _tax);
}
}
//Complex implementation of IProduct
public class MarylandProduct : IProduct {
private double _price;
private double _tax;
public double price {
get { return _price; }
set { _price = value; }
}
public double tax {
get { return _tax; }
set { _tax = value; }
}
public double calculateCost() {
if (_price <= 100) return _price + (_price * _tax);
else {
double returnValue = 100 + (100 * _tax); //use tax rate for first 100
returnValue += (_price - 100) + ((_price - 100) * 0.05); //use a flat rate of 0.05 for everything over 100
return returnValue;
}
}
}
我已经开始编写具有以下内容的单元测试:
[TestMethod]
[HostType("Moles")]
public void molesCalculateCostforMarylandProduct() {
//Assign
MMarylandProduct marylandProduct = new MMarylandProduct();
marylandProduct.priceGet = () => 1000;
marylandProduct.taxGet = () => 0.07;
const double EXPECTED = 1052;
//Act
double actual = marylandProduct.Instance.calculateCost();
//Assert
Assert.AreEqual(EXPECTED, actual);
}
我希望能够在我的单元测试中调用 MarylandProduct 或 SimpleProduct 的计算成本方法。通常从数据库中获取价格和税金,但我已经这样做了,以便对这些值进行存根处理,以避免与数据库或服务或提供这些值的其他任何东西耦合。归根结底是我想编写一个单元测试来测试calculateCost() 的功能,而不必在单元测试中存根该方法,因为我知道在两年内MarylandProduct 中的逻辑会改变。
因此,例如,一旦我运行了这个测试,我应该能够进入并更改MarylandProduct.calculateCost() 的代码,以添加“奢侈税”,即在超过 750 的任何价格上加 50。如果我这样做,我知道我的单元测试将失败,因为预期值为 1052,而现在 MarylandProduct 返回的值与预期值不同。
我是不是走错了路?我只是缺少 TDD 的精神吗? 感谢您的帮助。
编辑:(添加我尝试过的其他模拟框架)
[TestMethod]
public void rhinoMockCalculateCostForMarylandProduct() {
//assign
IProduct marylandProduct = MockRepository.GenerateMock<IProduct>();
marylandProduct.Stub(price => price.price).Return(1000);
marylandProduct.Stub(tax => tax.tax).Return(0.07);
const double EXPECTED = 1052;
//act
double actual = marylandProduct.calculateCost();
//assert
Assert.AreEqual(EXPECTED, actual);
}
[TestMethod]
public void moqCalculateCostForMarylandProduct() {
//assign
var marylandProduct = new Mock<IProduct>();
marylandProduct.Setup(price => price.price).Returns(1000);
marylandProduct.Setup(tax => tax.tax).Returns(0.07);
const double EXPECTED = 1052;
//act
double actual = ((MarylandProduct)marylandProduct.Object).calculateCost();
//assert
Assert.AreEqual(EXPECTED, actual);
}
我想避免在单元测试和类的实现中放置重复的代码,因为如果类中的代码发生更改,那么单元测试仍然会通过,因为它没有被更改。我知道预计会在单元测试中做出这种改变,但是当你有大量的单元测试时,这种设计是否可以接受?在您的单元测试和实现中存在重复代码?
【问题讨论】:
-
如果被测方法的行为发生变化,您的测试也必须如此。如果 calculatecost 的输出发生变化,那么很自然地期望你的断言会失败,因为它不再准确,因此你将不得不更新你的测试。你不能把你的蛋糕也吃掉
-
我看不出有什么理由不使用普通的存根或模拟而不是使用 Moles?
-
@jflood.net 是的,但是我想避免重复代码(单元测试中的 1 个,实现中的 1 个)只是因为我在 BO 中更新了它,单元测试仍然会通过,因为代码没有在单元测试中没有改变,它仍然会通过。这是我想要避免的。
-
@Tim Mahy,你能举个例子让我接受吗?我也有针对 rhinoMock 和 Moq 的单元测试,我将更新问题
-
所以你是说测试甚至没有执行业务对象逻辑?只是它的副本?首先,你的 bizzy 对象不应该依赖于 db。查找存储库模式。您的域应该是持久的无知。这样您就不需要存根获取价格和税收。
标签: c# unit-testing moq rhino-mocks moles