我正在使用moq和nunit,我的测试不会给我一个失败或通过。它说它没有默认构造函数。我怀疑我没有正确地将我的界面注入构造函数。
DonorManagementTests
[TestFixture]
public class DonorManagementTests
{
private readonly Mock<IValidation> _mockValidation;
private readonly DonorManagement _donorManagement;
public DonorManagementTests(IValidation validation)
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
DonorManagement
public class DonorManagement : IDonor
{
private readonly IValidation _validation;
public DonorManagement(IValidation validation)
{
_validation = validation;
}
public virtual decimal GiftAidAmount(decimal donationAmount)
{
const decimal gaRatio = 17.5m / (100 - 17.5m);
return _validation.ValidateDonation(donationAmount) * gaRatio;
}
}
我需要在代码中更改哪些想法?
答案 0 :(得分:5)
您的测试类必须有默认的C&#39; tor。
将您的测试类更改为:
[TestFixture]
public class DonorManagementTests
{
private Mock<IValidation> _mockValidation;
private DonorManagement _donorManagement;
[SetUp]
public TestInit()
{
_mockValidation = new Mock<IValidation>();
_donorManagement = new DonorManagement(_mockValidation.Object);
}
[Test, Description("View correct gift aid to two decimal places")]
public void DonorViewGiftAid()
{
const int donation = 20;
_mockValidation.Setup(x => x.ValidateDonation(donation)).Returns(20.00m);
var res = _donorManagement.GiftAidAmount(donation);
Assert.IsInstanceOf(typeof (decimal), res);
_mockValidation.Verify(x => x.ValidateDonation(donation), Times.Once);
}
}
现在每个测试都将被隔离,您将能够执行测试。
答案 1 :(得分:1)
NUnit文档(http://www.nunit.org/index.php?p=testFixture&r=2.5)说: