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
124
votes
10 answers

How to mock IConfiguration.GetValue

I tried in vain to mock a top-level (not part of any section) configuration value (.NET Core's IConfiguration). For example, neither of these will work (using NSubstitute, but it would be the same with Moq or any mock package I believe): var config…
dudeNumber4
  • 4,217
  • 7
  • 40
  • 57
53
votes
1 answer

NSubstitute: Checking received methods with array arguments

I want to verify that a method on my NSubstitute mock is called with a particular array argument. Say the interface, IProcessor, has a method void ProcessSomething(Foo[] something]). Say my class under test is named Commander. I set up my test like…
Jimothy
  • 9,150
  • 5
  • 30
  • 33
42
votes
7 answers

NSubstitute DbSet / IQueryable

So EntityFramework 6 is a lot better testable then previous versions. And there are some nice examples on the internet for frameworks like Moq, but the case is, I prefer using NSubstitute. I've got the "non-query" examples translated to work with…
s.meijer
  • 3,403
  • 3
  • 26
  • 23
41
votes
5 answers

Is it recommended to mock concrete class?

Most of the examples given in mocking framework website is to mock Interface. Let say NSubstitute that I'm currently using, all their mocking examples is to mock interface. But in reality, I saw some developer mock concrete class instead. Is it…
DonDon
  • 557
  • 1
  • 4
  • 13
36
votes
5 answers

NSubstitute - mock throwing an exception in method returning Task

Using NSubstitute, how do you mock an exception being thrown in a method returning a Task? Let's say our method signature looks something like this: Task> GetAllAsync(); Here's how NSubstitute docs say to mock throwing exceptions for…
Brandon
  • 1,566
  • 2
  • 14
  • 22
35
votes
3 answers

What are NSubstitute limitations, specially vs MOQ?

I'm about to take a decision about the mocking library for my next project. and because I'm new to those libraries I made a quick search I found that MOQ is much popular than NSubstitute and I expect more help from the community specially here at…
George Botros
  • 4,127
  • 3
  • 28
  • 50
27
votes
1 answer

NSubstitute: How to access actual parameters in Returns

I would like to access actual parameter in NSubstitute Returns method. For example: var myThing = Substitute.For() myThing.MyMethod(Arg.Any).Returns( + 1) Using NSubstitute what should I write in place of…
g.pickardou
  • 32,346
  • 36
  • 123
  • 268
27
votes
8 answers

Check calls Received() for async method

When I run the following code: [Test] public async Task Can_Test_Update() { var response = await _controller.UpdateAsync(Guid.NewGuid()); response.Valid.Should().BeTrue(); _commands.Received().UpdateAsync( Arg.Is( …
letitbe
  • 657
  • 1
  • 5
  • 18
26
votes
3 answers

NSubstitute - Received for async - “call is not awaited”warning

I am trying to verify that an asynchronous method was called with the correct parameters. However, I get the warning: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the…
JBond
  • 3,062
  • 5
  • 27
  • 31
23
votes
3 answers

NSubstitute - Check arguments passed to method

We are currently in the process of moving from RhinoMocks to NSubstitute. I have a method that takes an object of type DatabaseParams. This class has the following structure (simplified): public class DatabaseParams { public string…
JBond
  • 3,062
  • 5
  • 27
  • 31
19
votes
4 answers

NSubstitute ILogger .NET Core

I am trying to write unit tests around my exception handling so that I can verify the my logger is properly logging the exception. I am using NSubstitute as a mocking framework and Microsoft.Extensions.Logging.ILogger I have to following for my…
Nick Dichiaro
  • 330
  • 1
  • 2
  • 10
19
votes
1 answer

NSubstitute: Difference between Substitute.For<> and Substitute.ForPartsOf

I'm using NSubstitute. I have to fake a class and cannot dig out the difference of Substitute.For<...>() and Substitute.ForPartsOf<...>. I already read the documentation but don`t get the point, where the two behave different.
scher
  • 1,813
  • 2
  • 18
  • 39
18
votes
4 answers

NSubstitute mock extension method

I want to do mock extension method, but it does not work. How can this be done? public static class RandomExtensions { public static IEnumerable NextInt32s(this System.Random random, int neededValuesNumber, int minInclusive, int…
romanshoryn
  • 195
  • 1
  • 1
  • 4
18
votes
2 answers

Example of how to use AutoFixture with NSubstitute

I use NSubstitute a lot. And I love it. I am just looking into AutoFixture. It seems great! I have seen AutoFixture for NSubstitute and seen a few examples in Moq on how to use this feature. But I can't seem to translate it into NSubstitute. I…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
17
votes
2 answers

Can I get NSubstitute to auto mock my concrete classes?

I have an interface which I am mocking with 'NSubstitute' which contains properties that return concreate classes, that is the return value is not an interface. e.g public interface ISomething { SomeObj First { get; } SomeObj Second { get;…
karmasponge
  • 1,169
  • 2
  • 12
  • 26
1
2 3
41 42