determining stub identity/uniqueness

16 views
Skip to first unread message

mike wardle

unread,
Nov 8, 2013, 9:43:52 AM11/8/13
to rhino...@googlegroups.com
I am trying to determine if the return from a stubbed method is used as the parameter for another method.  The problem I have is that I have multiple stubbed objects of the same type in the same scope tat could all potentially be used as the parameter, so I do not see how to determine which object is actually being used:

var provider = MockRepository.GenerateStub<IProv>();
var a = MockRepository.GenerateStub<IObj>();
var b = MockRepository.GenerateStub<IObj>();
prov.Stub(x => x.Method1()).Return(a);
prov.Stub(x => x.Method2()).Return(b);
var consumer = MockRepository.GenerateMock<ICon>();
consumer.Expect(x => x.Method3(Arg<IObj>.Is.Equal(a));
var sut = new Sut(provider, consumer);
sut.DoWork();
consumer.VerifyAllExpectations();

where Sut.DoWork()
{
  var a = provider.Method1();
  var b = provider.Method2();
  consumer.Method3(a);
}

Given this, it does not confirm if a or b is passed into Method3 on the consumer...

Reginald Blue

unread,
Nov 8, 2013, 11:49:59 AM11/8/13
to rhino...@googlegroups.com
wait... so, if you changed Sut.DoWork to say "consumer.Method3(b)", then VerifyAllExpectations does not throw an error thus failing the test?

I would have assumed it would, though I might be getting confused with the way Moq works instead of Rhino Mocks...


--
You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rhinomocks+...@googlegroups.com.
To post to this group, send email to rhino...@googlegroups.com.
Visit this group at http://groups.google.com/group/rhinomocks.
For more options, visit https://groups.google.com/groups/opt_out.

mike wardle

unread,
Nov 9, 2013, 4:02:11 AM11/9/13
to rhino...@googlegroups.com
Yes, as a and b are anonymous stubs using eitherbwithin the concrete implementation passes the test which is not what I want.

Patrick Steele

unread,
Nov 9, 2013, 9:22:57 AM11/9/13
to rhino...@googlegroups.com
Your code works as-is.  Here's a complete test case that fails because I changed your code to pass "b" to Method3() instead of "a".  If you change it to pass "a", it passes (as expected):

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {

            var provider = MockRepository.GenerateStub<IProv>();
            var a = MockRepository.GenerateStub<IObj>();
            var b = MockRepository.GenerateStub<IObj>();
            provider.Stub(x => x.Method1()).Return(a);
            provider.Stub(x => x.Method2()).Return(b);

            var consumer = MockRepository.GenerateMock<ICon>();
            consumer.Expect(x => x.Method3(Arg<IObj>.Is.Equal(a)));

            var sut = new Sut(provider, consumer);
            sut.DoWork();
            consumer.VerifyAllExpectations();
        }
    }

    public class Sut
    {
        private readonly IProv provider;
        private readonly ICon consumer;

        public Sut(IProv provider, ICon consumer)
        {
            this.provider = provider;
            this.consumer = consumer;
        }

        public void DoWork()

        {
            var a = provider.Method1();
            var b = provider.Method2();
            consumer.Method3(b);           
        }
    }

    public interface ICon
    {
        void Method3(IObj obj);
    }

    public interface IObj
    {
    }

    public interface IProv
    {
        IObj Method1();
        IObj Method2();
    }

On Fri, Nov 8, 2013 at 9:43 AM, mike wardle <mgwa...@googlemail.com> wrote:

--

mike wardle

unread,
Nov 11, 2013, 4:12:18 AM11/11/13
to rhino...@googlegroups.com
you are right, that does work exactly as expected and desired, strange that I was getting incorrect functionality, must have been some mistake in my original code that I didnt reproduce in the simplified example I posted her.  Thank you for your help.
Reply all
Reply to author
Forward
0 new messages