0

Moqv4.13 とを使用していC#ます。MethodAのインターフェースでメソッドをテストしようとしていますMyClass : IMyInterfaceMethodA別のインターフェイス ( ) のメソッドの呼び出しがあり、IAnotherそれらをセットアップしました。これらはうまく設定されているようです。

MethodAMethodBまた、 ( on でIMyInterface) およびMethodC( public であり、 on ではなくIMyInterface) inを呼び出しますMyClass

セットアップしましMethodBたが、テスト中に実際のコードMethodBが呼び出されたようです。

に関してはMethodC、その設定方法がわかりません。

public interface MyInterface 
{
    int MethodA();
    int MethodB();
}
public class MyClass : MyInterface 
{
    public MyClass(IAnother b) {...}
    
    public int MethodB(){...}
    public int MethodC(){...}

    public int MethodA()
    {
        ...
        var x = b.MethodX();
        ...
        var a = MethodB();
        ...
        var b = MethodC()

        return a + b + x;
    }
}
public MyInterfaceHarness
{
   Mock<IAnother> _anotherMock;

   public MyInterfaceHarness() 
   {
       _anotherMock = new Mock<IAnother>();
       Mocker = new AutoMocker();
   }

   public AutoMocker Mocker {get;}
   
   public MyClass MethodAHarness()
   {
       Mocker.Use(_anotherMock)  // Mocker is Automocker
       _anotherMock.Setup(m => m.MethodX()).Returns(5); // this seems to be setup fine

      //here I want to setup invocations to MethodB and MethodC
      ....

      Mocker.CreateInstance<MyClass>();
   }
}
[TestFixture]
public MyInterfaceTest 
{
    private  MyInterfaceHarness _harness;
    private AutoMocker _mocker;

    [SetUp]
    public void SetUp()
    {
       _harness = new MyInterfaceHarness();
       _mocker = _harness.Mocker();
    }

    [Test]
    public void Should_Test_MethodA()
    {
        var mi = _harness.MethodAHarness();

        // use _mocker to setup other invocations that need verify            
        ...

        var i = mi.MethodA();
       
       //asserts
       ...
        
    }
}
4

0 に答える 0