如何模拟接口中接口的只读 属性 已经被模拟
How to mock read only property of interface in interface is already mocked
public interface IPerson
{
SalaryCalculator SalaryCalculator { get; }
}
public interface ISalaryCalculator
{
}
public class SalaryCalculator : ISalaryCalculator
{
public int JoiningDate { get; set; }
public SalaryCalculator(int joiningDate)
{
JoiningDate = joiningDate;
}
}
[TestFixture]
public class PersonFixture
{
IPerson person;
[SetUp]
public void SetUp()
{
person = Substitute.For<IPerson>();
}
[Test]
public void TestPerson()
{
person.SalaryCalculator.Returns(new SalaryCalculator());
OR
person.SalaryCalculator.Returns(Substitute.For<ISalaryCalculator>());
}
}
我尝试使用以上两个选项进行模拟,但不起作用,
NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException :
Can not return value of type ISalaryCalculatorProxy for
IPerson.get_SalaryCalculator (expected type SalaryCalculator).
在这种情况下我们如何模拟?
异常指示模拟成员应该是什么 returning。
...expected type SalaryCalculator
return 实际的 class 因为那是模拟接口成员 returns.
SalaryCalculator instance = new SalaryCalculator(1);
person.SalaryCalculator.Returns(instance);
public interface IPerson
{
SalaryCalculator SalaryCalculator { get; }
}
public interface ISalaryCalculator
{
}
public class SalaryCalculator : ISalaryCalculator
{
public int JoiningDate { get; set; }
public SalaryCalculator(int joiningDate)
{
JoiningDate = joiningDate;
}
}
[TestFixture]
public class PersonFixture
{
IPerson person;
[SetUp]
public void SetUp()
{
person = Substitute.For<IPerson>();
}
[Test]
public void TestPerson()
{
person.SalaryCalculator.Returns(new SalaryCalculator());
OR
person.SalaryCalculator.Returns(Substitute.For<ISalaryCalculator>());
}
}
我尝试使用以上两个选项进行模拟,但不起作用,
NSubstitute.Exceptions.CouldNotSetReturnDueToTypeMismatchException : Can not return value of type ISalaryCalculatorProxy for IPerson.get_SalaryCalculator (expected type SalaryCalculator).
在这种情况下我们如何模拟?
异常指示模拟成员应该是什么 returning。
...expected type SalaryCalculator
return 实际的 class 因为那是模拟接口成员 returns.
SalaryCalculator instance = new SalaryCalculator(1);
person.SalaryCalculator.Returns(instance);