犀牛嘲笑回归存根?

时间:2011-06-05 18:41:29

标签: rhino-mocks

以下是否可行 -

var stub1 = MockRepository.GenerateStub<stub1>();
var stub2 = MockRepository.GenerateStub<stub2>();

int returnValue = 1;

stub2.Stub(x => x.stub2Method(Arg<int>.Is.Anything).Return(returnValue).Repeat.Once();
Stub1.Stub(x =>x.stub1Method(Arg<int>.Is.Anything)).Repeat.Once().Return(stub2);

即。可以从存根中返回具有预期的存根吗?

在我的代码中,当stub2.stub2Method调用stub1.stub1Method时,会返回null而不是returnValue

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

是的,虽然这可能取决于你的目标。

以下是一个例子:

public class Class1
{
    public virtual IClass2 Stub1Method()
    {
        throw new NotImplementedException();
    }
}

public interface IClass2
{
    int StubMethod2();
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var stub1 = MockRepository.GenerateStub<Class1>();
        var stub2 = MockRepository.GenerateStub<IClass2>();
        var expected = 1;
        stub2.Stub(s => s.StubMethod2()).Repeat.Once().Return(1);
        stub1.Stub(s => s.Stub1Method()).Return(stub2).Repeat.Once();
        var result = stub1.Stub1Method().StubMethod2();
        Assert.AreEqual(expected, result);
    }
}