nsubstitute method returns

This value will be returned every time this call is made. Returns() will only apply to this combination of arguments, so other calls to this method will return a default value instead.

//Make a call return 3:
calculator.Add(1, 2).Returns(3);
Assert.AreEqual(calculator.Add(1, 2), 3);
Assert.AreEqual(calculator.Add(1, 2), 3);

//Call with different arguments does not return 3
Assert.AreNotEqual(calculator.Add(3, 6), 3);

Return for any args
A call can be configured to return a value regardless of the arguments passed using the ReturnsForAnyArgs() extension method.

calculator.Add(1, 2).ReturnsForAnyArgs(100); 
Assert.AreEqual(100, calculator.Add(1, 2));
Assert.AreEqual(100, calculator.Add(-7, 15));
Tip! You can also use the default C# keyword for better readability:

calculator.Add(default, default).ReturnsForAnyArgs(100);
The same behaviour can also be achieved using argument matchers: it is simply a shortcut for replacing each argument with Arg.Any<T>().

ReturnsForAnyArgs() has the same overloads as Returns(), so you can also specify multiple return values or calculated return values using this approach.

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source