semantic_c0d3r
semantic_c0d3r

Reputation: 521

Return a new object for each Returns() in NSubstitute

I have created a substitute which mocks a web service interface for my unit testing which includes the following method definition:

public Message Invoke(Message input)

This method is called using:

var reply = webService.Invoke(messageObject)

When I make multiple calls to the same method, it is throwing the following exception:

System.InvalidOperationException : This message cannot support the operation because it has been read.

Here is my Nsubstitute mock code:

outputMessageObj = GetResponseMessage()
wsMock.Invoke(Arg.Any<Message>()).Returns(outputMessageObj)

How do I ensure that a new outputMessage object is returned each time the call is made?

Upvotes: 8

Views: 2643

Answers (1)

semantic_c0d3r
semantic_c0d3r

Reputation: 521

Got it, just use a lambda to invoke a method which returns a new Message object each time:

wsMock.Invoke(Arg.Any<Message>()).Returns(x => GetResponseMessage())

Upvotes: 11

Related Questions