英文:
Test a private method with a factory
问题
我是新手单元测试,正在为一个调用内部私有方法的方法创建单元测试。这个私有方法还调用了工厂中的一个函数。
private string FingerPrintCreator(string any)
{
using var calculator = mFingerprintCalculatorFactory.Create();
return calculator.Compute(any);
}
public string AnyFunction()
{
var fingerprint = FingerPrintCreator("xxxx");
return fingerprint + "YYY";
}
到目前为止,我有以下内容:
private class TestContext
{
public readonly Mock<IFingerprintCalculatorFactory> FingerprintCalculatorFactoryMock = new();
public TestContext()
{
FingerprintCalculatorFactoryMock.Setup(factory => factory.Create());
}
}
[Test]
public string Test_function()
{
string Any = "XXX";
var testContext = new TestContext();
testContext.FingerprintCalculatorFactoryMock.Setup(factory => factory.Create()).Returns(It.IsAny<IFingerprintCalculator>());
var result = await CreateProvider(testContext).AnyFunction;
}
在实例化CreateProvider后,我在return calculator.Compute(any);
处得到System.NullReferenceException异常。
如何进行测试?任何建议将不胜感激。
英文:
I am new to Unit testing I am creating the unit tests for a method which calls a private method inside, this private method also calls a function from a factory
private string FingerPrintCreator(string any)
{
using var calculator = mFingerprintCalculatorFactory.Create();
return calculator.Compute(any);
}
public string AnyFunction()
{
var fingerprint=FingerPrintCreator("xxxx");
return fingerprint+"YYY";
}
so far I have:
private class TestContext
{
public readonly Mock<IFingerprintCalculatorFactory> FingerprintCalculatorFactoryMock = new();
public TestContext()
{
FingerprintCalculatorFactoryMock.Setup(factory => factory.Create());
}
}
[Test]
public string Test_function()
{
string Any = "XXX";
var testContext = new TestContext();
testContext.FingerprintCalculatorFactoryMock.Setup(factory => factory.Create()).Returns(It.IsAny<IFingerprintCalculator>());
var result = await CreateProvider(testContext).AnyFunction;
}
after CreateProvider is instantiated I get System.NullReferenceException at calculator from return calculator.Compute(any);
How can I test it? Any advice would be very appreciated
答案1
得分: 2
你需要更改你的设置,以返回一个实际的实例(或另一个模拟对象),而不是 It.IsAny<IFingerprintCalculator>()
(It.IsAny<T>
返回一个 default(T)
,在这种情况下是 null
- Console.WriteLine(It.IsAny<IFingerprintCalculator>() is null);
会打印出 True
):
testContext.FingerprintCalculatorFactoryMock
.Setup(factory => factory.Create())
.Returns(new ....); // 或创建另一个模拟对象并返回它
英文:
You need to change your setup to return an actual instance (or another mock) instead of It.IsAny<IFingerprintCalculator>()
(It.IsAny<T>
returns a default(T)
which is null
in this case - Console.WriteLine(It.IsAny<IFingerprintCalculator>() is null);
prints True
):
testContext.FingerprintCalculatorFactoryMock
.Setup(factory => factory.Create())
.Returns(new ....); // or create another mock and return it
答案2
得分: 1
你不直接测试私有方法。你直接测试你的公有方法,它们会间接使用你的私有方法 - 因此你的私有方法会被间接测试。
英文:
You don't test private methods directly. You test your public methods directly and they will make use of your private methods - so your private methods will be tested indirectly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论