Questions tagged [nsubstitute]

NSubstitute is a .NET mocking framework. It creates substitutes of types for testing that can act as both mocks (can check calls were received) and stubs (can configure results for calls).

NSubstitute is a dynamic .NET mocking library, compatible with netstandard-1.3 and .NET 4.5 and above (earlier versions support back to .NET 3.5).

Using a "fluent" syntax, it creates substitutes of types for testing that can be configured to return specific results for calls, or can be queried about the calls they did receive. As this combines the behaviour of "mocks" and "stubs", NSubstitute uses the term "substitute" to describe these test doubles.

629 questions
0
votes
2 answers

Is there a way to cache an Arg.Is<> definition for use in both the "Arrange" and "Act" parts of a test?

I have a test that looks like this: [Test] public void Blah() { // Arrange // ... var thing = new Thing(); mockRouter.Route(Arg.Is>(x => x != null && x.Subject != null &&…
Kit
  • 20,354
  • 4
  • 60
  • 103
0
votes
1 answer

Is it possible to return a substitute from a substitute with NSubstitute?

interface IA { B Foo(); } interface IB { // more code } var a = Substitute.For(); var b = Substitute.For(); a.Foo().Returns(b); Is that possible?
Daniel Gartmann
  • 11,678
  • 12
  • 45
  • 60
0
votes
1 answer

Expression causes mock repository to return null

I'm trying to test a service method but in order to do that I have to mock my ReportRepository. Everything works fine except the call to the Include method makes the mock return null. The following returns the expected report: var report = new…
Neil Smith
  • 2,565
  • 1
  • 15
  • 18
0
votes
0 answers

Testing Arg.Any Task

I am trying to test the orders of which the methodes are called. The issue I have is that I can't seem to find a good way to mock a Task object. I am using NSubstitute and Entity Framwork 6. This is sort of what my code looks…
0
votes
1 answer

NSubstitute, out Parameters and conditional Returns

I have a scenario not dissimilar to the one below that I would like to mock up in NSubstitute... public interface IGrabSomeData { bool GrabThatData(string filename, out byte[] data); } ...with this interface I would like it to take a filename…
SeanCocteau
  • 1,838
  • 19
  • 25
0
votes
1 answer

NSubstitute cannot setup return value (CouldNotSetReturnException)

I have an interface (called IRepository) that has a method on it like this: IEnumerable ExecuteStoredProcedure(string functionName, params Tuple[] parameters); I am trying to…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
0
votes
1 answer

Resharper displaying NSubstitute methods as errors

We have upgraded to Resharper 7.1 with VS2012. However Resharper seems to display NSubstitute methods as unrecognised in red as shown in the diagram below with Arg.Any<>: Does anyone know how to resolve this? Note: The codes compiles successfully,…
bstack
  • 2,466
  • 3
  • 25
  • 38
0
votes
1 answer

A simple way to register a fake for a concrete class using AutofacContrib.NSubstitute

A class that is resolved as builder.Resolve Can be faked like this (for testing) builder.RegisterType().As(); But what if my class is resolved as builder.Resolve How to I fake this in autofac…
developer747
  • 15,419
  • 26
  • 93
  • 147
0
votes
1 answer

How to pass Interface as parameter for Controller class in Substitute

I am new to nSubstitute. And I am writing test method for my controller class. I have a TestMethod called GetDefaultStateTest() which having Substitute class as shown below [TestMethod] public void GetDefaultStateTest() { var…
user792223
  • 61
  • 3
  • 8
0
votes
1 answer

Code analysis CA0001 error for project using NSubstitute

I have a (.NET 4) test project which references (the .NET 4) NSubstitute.dll. When I run CodeAnalysis against the project I get a number of CA0001 errors: Running Code Analysis... MSBUILD : error : CA0001 : The following error was encountered while…
GarethOwen
  • 6,075
  • 5
  • 39
  • 56
0
votes
1 answer

Asp.net MVC Test of Create action method with NUnit and Nsubstitute always fails

I have this controller: [HttpPost] public ActionResult Create(Company company) { // try to save the record if (ModelState.IsValid) { // create the command var…
0
votes
1 answer

Is there a way to get recursive mocks on a class return value with NSubstitute

NSubstitute says this in its docs: methods that return an interface [...] will automatically return substitutes themselves. That is enough usually. However, when I do this: TestMethod: IUnityContainer unity =…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
0
votes
3 answers

How do you mock a subclass of a built-in type (e.g. TextBox)?

We're using NSubstitute, so I think we need a single interface to mock. We also have code that references the superclass and using polymorphism. Ideas I had.. Create an interface with both the methods I've added and the existing ones (e.g. Value and…
Sam
  • 581
  • 4
  • 17
-1
votes
1 answer

Unit Test the a string that is used to pass to another function using XUnit and NSubstitute in ASP.NET Core

So I have a function like this that will call other function and pass in a string that will be returned based on different condition. They will always use a normal string, however if certain conditions are met then the special string will be used to…
Noelia
  • 89
  • 10
-1
votes
1 answer

Nsubstitute return different based on the object properties

I have codes like below. The first call of save(), I want it to throw an exception. And the second call when obj.field="ok", I do not want it to throw exception. How do i achieve this through Nsubstitute? Try { //Source codes workItem.save()…
Yu Lan
  • 1
  • 2
1 2 3
41
42