Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

c# - 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?

question from:https://stackoverflow.com/questions/8343709/rhino-mocks-receive-argument-modify-it-and-return

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...