Moq
v4.13 とを使用していC#
ます。MethodA
のインターフェースでメソッドをテストしようとしていますMyClass : IMyInterface
。
MethodA
別のインターフェイス ( ) のメソッドの呼び出しがあり、IAnother
それらをセットアップしました。これらはうまく設定されているようです。
MethodA
MethodB
また、 ( 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
...
}
}