开发者

Rhino Mocks Returning a stub?

Is the following possible -

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

int returnValue = 1;

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

i.e. can a stub with expectations be returned from a stub?

In my code, when stub2.stub2Method is called from stub1.stub1Method, null is returned instead of returnValue.

Any idea why?


Yes, though it may depend on what you are stubbing out.

As an example the following works:

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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜