Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks receive argument, modify it and return?

I'm trying to write something like this:

myStub.Stub(_ => _.Create(Arg<Invoice>.It.Anything)).Callback(i => { i.Id = 100; return i; }); 

I want to get actual object that passed to mock, modify it and return back.

Is this scenario possible with Rhino Mocks?

like image 841
Alexander Beletsky Avatar asked Dec 01 '11 15:12

Alexander Beletsky


1 Answers

You could use the WhenCalled method like this:

myStub     .Stub(_ => _.Create(Arg<Invoice>.Is.Anything))     .Return(null) // will be ignored but still the API requires it     .WhenCalled(_ =>      {         var invoice = (Invoice)_.Arguments[0];         invoice.Id = 100;         _.ReturnValue = invoice;     }); 

and then you can create your stub as such:

Invoice invoice = new Invoice { Id = 5 }; Invoice result = myStub.Create(invoice); // at this stage result = invoice and invoice.Id = 100 
like image 197
Darin Dimitrov Avatar answered Sep 21 '22 21:09

Darin Dimitrov