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?
To set a return value for a method call on a substitute, call the method as normal, then follow it with a call to NSubstitute's Returns() extension method. var calculator = Substitute. For<ICalculator>(); calculator.
NSubstitute is a great library for mocking objects for Test Driven Development (TDD) in . NET.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With