无返回值方法,RhinoMock的例子是这么做的:
demo.VoidThreeArgs(0, "", 0f); LastCall.On(demo).Callback<int, string, float>(ThreeArgsAreSame); mocks.Replay(demo);
无参数方法:
INameSource nameSource = (INameSource)mocks.StrictMock(typeof(INameSource)); Expect.Call(nameSource.CreateName(null,null)).IgnoreArguments(). Do(new NameSourceDelegate(Formal)); mocks.ReplayAll(); string expected = "Hi, my name is Ayende Rahien"; string actual = new Speaker("Ayende", "Rahien", nameSource).Introduce(); Assert.Equal(expected, actual);
最需要的是忽略参数,IgnoreArguments()正可以达到目的
Do里面是委托的基类型,但是需要注意,方法的参数数目和委托的参数数目需要一致。
否则会报异常InvalidOperationException callback arguments didn`t match the method arguments.