New Developer
New Developer

Reputation: 3305

C# Mock method return value evaluate at the actual execution

I'm using Nunit testing with Rhino-Mocks for my code unit testing.
My code is like below;

_mockHandler.Expect(m => m.DoSomething(Arg<string>.Is.Anything, Arg<string>.Is.Anything))
            .Return(myList["XXX"]);

Content of the myList is updating from a thread running. Once the above line executing value of myList["XXX"] is empty. But at the time of this method invoke from the code myList["XXX"] is having a value.

Is there any way to configure, refer to the actual value when calling the method?

Upvotes: 2

Views: 726

Answers (1)

Old Fox
Old Fox

Reputation: 8725

The way to return value in runtime instead of record-time is to use WhenCalled or Do methods.

In your case it seems that Do is the right solution:(If you wants to know when to use Do or WhenCalled read my answer)

_mockHandler.Expect(m => m.DoSomething(Arg<string>.Is.Anything, 
                                       Arg<string>.Is.Anything))
            .Do(new Func<string,string,string>((s, s1) => myList["XXX"]));

In the above example (s, s1) => myList["XXX"]) will execute when you call DoSomething and the value in myList["XXX"] will return.

Upvotes: 3

Related Questions