测试一个带有工厂的私有方法

huangapple go评论57阅读模式
英文:

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(&quot;xxxx&quot;);

    return fingerprint+&quot;YYY&quot;; 
  }

so far I have:

  private class TestContext
  {
    public readonly Mock&lt;IFingerprintCalculatorFactory&gt; FingerprintCalculatorFactoryMock = new();
    public TestContext()
    {
      FingerprintCalculatorFactoryMock.Setup(factory =&gt; factory.Create());
    }
  }
  [Test]
  public string Test_function()
  {
    string Any = &quot;XXX&quot;;
    var testContext = new TestContext();
    testContext.FingerprintCalculatorFactoryMock.Setup(factory =&gt; factory.Create()).Returns(It.IsAny&lt;IFingerprintCalculator&gt;());
    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&lt;IFingerprintCalculator&gt;() (It.IsAny&lt;T&gt; returns a default(T) which is null in this case - Console.WriteLine(It.IsAny&lt;IFingerprintCalculator&gt;() is null); prints True):

testContext.FingerprintCalculatorFactoryMock
    .Setup(factory =&gt; 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.

huangapple
  • 本文由 发表于 2023年7月13日 21:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定