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
15
votes
4 answers

NSubstitute - Testing for a specific linq expression

I am using the repository pattern in an MVC 3 application I am currently developing. My repository interface looks as follows: public interface IRepository where TEntity : IdEntity { void Add(TEntity entity); void Update(TEntity…
Ozzy
  • 964
  • 1
  • 10
  • 21
15
votes
2 answers

NSubstitute - mock out parameter behaviour for any parameter

I'm trying to mock IConfigurationProvider with NSubstitute. I need the method bool TryGet(string key, out string value) to return values for differing keys. So something like this: var configProvider =…
V0ldek
  • 9,623
  • 1
  • 26
  • 57
15
votes
2 answers

NSubstitute test works by itself, but throws Unexpected Matcher Argument in a suite

I have a unit test where I use .Returns() to return some sample data: [TestMethod] public void TestRetrieveElementsInVersion() { IRetrieveElementSequence component = Substitute.For(); …
Skip Saillors
  • 744
  • 13
  • 27
15
votes
2 answers

How can I create and populate my mock classes with Autofixture?

Currently I'm using EF6 to implement my repositories inside a UnitOfWork. I also have created an In-Memory mock implementations (MockUnitOfWork & MockRepository) so that I can use them in unit tests, however I now have to deal with the tedious…
WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
14
votes
3 answers

How stable is NSubstitute?

My company is looking to standardize on an Isolation Framework. I was looking at MS Stubs (cause Moles seemed cool and I thought I would keep it in the same framework). However, Stubs is not quite ready for prime time yet (it is still a bit buggy…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
14
votes
2 answers

NSubstitute mock a void method with out parameters

I am new to NSubstitute, I am trying to mock a void method with 2 out parameters and I am pretty sure I am doing it wrong. I have a CustomerDataAccess class that has a method with the following signature: void GetCustomerWithAddresses(int…
NathanFisherSdk
  • 153
  • 1
  • 2
  • 8
14
votes
1 answer

how to mock a property with private setter using NSubstitute

I am having a class "Example" with a property "data" which has a private setter and I would like to mock that data property Public class Example { public string data {get; private set;}} I would like to mock the data property using NSubstitute.…
KDKR
  • 365
  • 2
  • 3
  • 11
14
votes
1 answer

Returning the result of a method that returns another substitute throws an exception in NSubstitute

I have run into a weird issue while using NSubstitute a few times and although I know how to work around it I've never been able to explain it. I've crafted what appears to be the minimum required test to prove the problem and it appears to be…
craftworkgames
  • 9,437
  • 4
  • 41
  • 52
12
votes
3 answers

NSubstitute to return a Null for an object

I am new to unit testing and it sounds to me like it should be easy to get NSubstitute to be able to return null for a method but I cannot get it to work. I have tried this for a Get method that should return a Campaign method _campaigns =…
Manuel
  • 131
  • 1
  • 1
  • 6
12
votes
4 answers

NSubstitute multiple return sequence

I want to substitute object to return sequence of different objects. For example: var http = Substitute.For(); http.GetResponse(Arg.Any()).Returns(resourceString, resourceString2); http.GetResponse(Arg.Any()).Returns(x => {…
Alexandr Nikitin
  • 7,258
  • 2
  • 34
  • 42
11
votes
1 answer

How to use NSubstitute to mock a lazy class

//Assert Lazy notificationService = Substitute.For>(); Service target = new Service(repository,…
Kuroro
  • 1,841
  • 1
  • 19
  • 32
11
votes
2 answers

How to substitute Object.ToString using NSubstitute?

When I try to use NSubstitute 1.7.1.0 to define behaviour of Object.ToString (which is a virtual method), NSubstitute is throwing an exception. To reproduce: [Test] public static void ToString_CanBeSubstituted() { var o =…
Milan Gardian
  • 11,326
  • 5
  • 38
  • 45
11
votes
1 answer

Mocking Generic Method with NSubstitute

I have an interface with a number of generic methods. These methods perform operations based on the type of data that is passed in. How do I mock this with NSubstitute? At the moment, I had to resort to using a concrete class instead of a mock since…
ritcoder
  • 3,274
  • 10
  • 42
  • 62
11
votes
1 answer

NSubstitute with object as parameter in Received call

I am using NSubstitute for my Unit tests. I need to check that a object is send to a void method inside the method I am testing. I only need to check that the object is sent with one of the properties being a certain value. eg. ///The object in…
Captain0
  • 2,583
  • 2
  • 28
  • 44
10
votes
1 answer

How to mock IAuthorizationService in .net core 2.0

I'm using this resource authorization in my controller: var result = await _authorizationService.AuthorizeAsync(User, document, operation); I need to test my controller, and I need the authorization to pass in the test. I…
John-Luke Laue
  • 3,736
  • 3
  • 32
  • 60
1
2
3
41 42