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
6
votes
1 answer

How can I specify a failure message for an NSubstitute .Received call?

In NSubstitute, is it possible to specify a message that should be thrown if a Received fails? Something like the following: [Test] public void Should_execute_command() { var command = Substitute.For(); var something = new…
eliah
  • 2,267
  • 1
  • 21
  • 23
6
votes
3 answers

NSubstitute VerifyAll equivalent

Does NSubstitute have an equivalent to MOQ's VerifyAll call? I'd like to verify that all calls I expect to be received across all substitutes are actually called, ideally in a single TearDown method. I'm currently verifying each received call…
levelnis
  • 7,665
  • 6
  • 37
  • 61
5
votes
1 answer

NSubstitute: Checking DidNotReceive async method

Given an async method, how can NSubstitute check a call was not received? With NSubstitute, we can check a async method (or many) were received in order using: Received.InOrder(async () => { await client.SendAsync(Arg.Any()) }); What is…
0909EM
  • 4,761
  • 3
  • 29
  • 40
5
votes
2 answers

Cannot test ILogger Received with NSubstitute

I have a .Net Core 3 application and am trying to test calls to ILogger in my method: public class MyClass { private readonly ILogger _logger; public MyClass(ILogger logger) { _logger = logger; } …
Shevek
  • 3,869
  • 5
  • 43
  • 63
5
votes
2 answers

Importing NSubstitute into Unity project

I'm trying to use NSubstitute in my Unity project (Unity version 2019.2.2f1). No matter how to import it, my IDE, both Visual Studio and JetBrains Rider, would give an error when I try using NSubstitute saying NSubstitute is undefined. I have tried…
Dendi Maniac
  • 69
  • 1
  • 6
5
votes
1 answer

NSubstitute: Mocking properties

I created substitutes for Person and AddressBook classes in the unit Test. The AddressBook class contains properties of type Person and name: SamplePerson. public interface IAddressBook { Person SamplePerson { get; set; } } public class…
Piotr X
  • 107
  • 1
  • 9
5
votes
1 answer

how to mock AuthenticateAsync on AspNetCore.Authentication.Abstractions

i have an action on a controller which calls var result = await HttpContext.AuthenticateAsync(IdentityServerConstants.ExternalCookieAuthenticationScheme); i'm trying to mock this result in a unit test like…
ryanthescot
  • 327
  • 4
  • 16
5
votes
2 answers

NSubstitute ReturnsForAnyArgs is returning null but shouldn't

I have a problem in my test case, I am trying to mock the return of my ICacheProvider but it always returnsnull. [Fact] public void Raise_ShoultReturnTrue_IfItsInCache() { var cacheProvider = Substitute.For(); …
TiagoM
  • 3,458
  • 4
  • 42
  • 83
5
votes
1 answer

How do I mock a base method from the Controller class using the NSubstitue framework

I need a mock a method present in a base class when an Action method in the Controller class invoke it. Here is my Controller class below, the action method Index() calls the base method GetNameNodeStatus(). Now how can I mock the…
Kumaran Raj K
  • 365
  • 1
  • 11
5
votes
1 answer

How to mock indexer information for the returned object in NSubstitute

I want to mock indexer property return by the object. I am using NSubstitute for mocking. I have following scenario. In the sample I wanted to mock indexer property this[string symbolName] while returning from IFeedData.GetFeedData public class…
Sumit Deshpande
  • 2,155
  • 2
  • 20
  • 36
5
votes
2 answers

How do I unit test a repository that uses DbContext with NSubstitute?

I have a solution in which I have a Data project that contains an EF6 .edmx file, generated from an existing database. I split the entities into a separate Entities project, and have a Repositories project that references them both. I have added a…
Avrohom Yisroel
  • 8,555
  • 8
  • 50
  • 106
5
votes
1 answer

Does NSubstitute support ref parameters?

I have the following method signature in my iterface: void SetItem(ref AddressItem item); I do a parameter constraint like this: IAddAddressForm form = Substitute.For(); AddressItem item = null; form.SetItem(Arg.Is(item)); But…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
5
votes
1 answer

AutoFixture + NSubstitute throws NotASubstituteException for injected auto mock

I'm receiving the following Exception: NSubstitute.Exceptions.NotASubstituteException: NSubstitute extension methods like .Received() can only be called on objects created using Substitute.For() and related methods. ...when calling…
jameskind
  • 1,097
  • 2
  • 13
  • 28
5
votes
1 answer

Nsubstitute check if setter has been called

I have an interface with a property: public interface Filterable { Filter Filter { get; set; } } I have a method simelar to this one: public void SetTheFilter(Filterable filterable, Filter filter) { If (filter.IsActive) …
scher
  • 1,813
  • 2
  • 18
  • 39
5
votes
2 answers

Automocking with LightInject plus Nsubstitute, how?

I am new to both libraries and before committing to their usage on a large project I need clarification on my options for low-code effort automocking in my unit tests. After spending some time on Google I have concluded that, unlike some other…
camelCase
  • 1,549
  • 2
  • 16
  • 28