更新 1,更多细节
我的问题很难解释,对不起......
简单的问题...您如何使用 Moq 测试特定的实现?
interface IShipping
{
int CalculateCost();
}
class TruckShipping: IShipping
{
int CalculateCost()
{
return 9;
}
}
class Shipping: IShipping
{
int CalculateCost()
{
return 5;
}
}
class CalculateSomethingMore
{
IShipping _shipping;
CalculateSomethingMore(IShipping shipping)
{
// Here I want the TruckShipping implementation!
_shipping = shipping;
}
DoCalc()
{
return _shipping.CalculateCost * 2;
}
}
如果没有模拟,它可能看起来像(如果你不使用 DI)测试:
var truckShipping = new TruckShipping();
var advancedCalculation = CalculateSomethingMore(truckShipping);
var result = DoCalc();
var expected = 18;
Assert.IsTrue(result == expected);
NUnit、FluentAssertions、MbUnit、xUnit 等。没关系 :)
测试: var truckShipping = Mock.Of<IShipping> .... ? 我想测试 TruckShipping 的实现。
并将其注入CalculateSomethingMore。