"Arrogant" using Rhino Mocks

I have a situation that I have encountered several times, but have not found a good answer. Suppose I have a class like the following, where one method calls another in the same class:

public class Foo
{
    public int Bar()
    {
        if (Baz())
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }

    public virtual bool Baz()
    {
        // behavior to be mocked
    }
}

I want unit test the behavior of the Bar () method depending on the return values ​​of Baz (). If Baz () were in another class, I would call PartialMock to set mocking behavior in this class, but it does not work when PartialMock is used on the test class itself. Is there an easy way to do this? What am I missing?

I am using Rhino Mocks 3.5 and .NET 2.0.

+3
source share
1 answer

Baz. .NET 3.5, lambdas, .NET 2.0 , :

Foo f = MockRepository.GenerateStub<Foo>();
// lambda:
// f.Stub(x => x.Baz()).Return(true);
// anonymous delegate:
f.Stub(delegate(Foo x) { return x.Baz(); }).Return(true);
Console.WriteLine(f.Bar());
+3

Source: https://habr.com/ru/post/1725729/


All Articles