Or if you really need to grab the argument that was passed in you can do it
this way ...


    public class Foo
    {
        public String Label { get; set; }
    }

    public interface IFooService
    {
        string GetDescription(Foo foo);
    }

    public class TestMe
    {
        private readonly IFooService fooService;

        public TestMe(IFooService fooService)
        {
            this.fooService = fooService;
        }

        public string SomethingThatNeedsADescription(Foo foo)
        {
            var descr = fooService.GetDescription(foo);
            return descr;
        }
    }


    [TestFixture]
    internal class Test
    {
        [Test]
        public void DoStuff()
        {
            var fooArg = new Foo() {Label = "Passed Back By Mock"};
            var fooService = MockRepository.GenerateStub<IFooService>();

            fooService
                .Expect(s => s.GetDescription(fooArg))
                .WhenCalled((i) =>
                                {
                                    i.ReturnValue = (i.Arguments.First() as
Foo).Label;
                                })
                .Return("Value that gets overidden by WhenCalled(...)")
                .Repeat
                .Once();

            var testMe = new TestMe(fooService);
            var result = testMe.SomethingThatNeedsADescription(fooArg);

            fooService.AssertWasCalled(f => f.GetDescription(fooArg));
            Assert.That(result, Is.EqualTo("Passed Back By Mock"));
        }

    }


On 16 May 2011 14:47, Alex McMahon <[email protected]> wrote:

> you can add a .Return(null) and the WhenCalled will override this Return
> and satisfy the error check.
>
>
> On 16 May 2011 14:09, Giulio Petrucci <[email protected]> wrote:
>
>> Hi Gavin,
>>
>> first of all, thank you for your reply.
>>
>> On Mon, May 16, 2011 at 2:50 PM, Gavin van der Merwe
>> <[email protected]> wrote:
>> > Try this
>> >             var fooService = MockRepository.GenerateStub<IFooService>();
>> >             fooService.Stub(s =>
>> > s.GetDescription(Arg<Foo>.Is.Anything)).WhenCalled(
>> >                 (i) => i.ReturnValue = i.Arguments.First());
>> >             var testMe = new TestMe(fooService);
>>
>> At a glance, I'd say that in this way no return value is provided.
>> I've tested this suggestion and I have an error telling me
>> "Method 'IFooService.GetDescription(anything);' requires a return
>> value or an exception to throw." Any hint?
>>
>> Thanks again,
>> Giulio
>>
>> --
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Rhino.Mocks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/rhinomocks?hl=en.
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Rhino.Mocks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rhinomocks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to