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

Is it possible to mock a local method variable using NSubstitute?

E.g. I have a class with a Process method, in this method I set up a number of things e.g. public class messageProcessor { ... public string Process(string settings) { var elementFactory = new ElementFactory(); var strategyToUse = new…
Blingers
  • 789
  • 1
  • 9
  • 22
0
votes
0 answers

How to mock a method of the subject under test using AutoFixture / NSubsitute / NUnit?

I'm trying to write some unit tests using the Given-When-Then method. However, I don't seem to be able to mock a method inside the SUT that is called from the invoked method in the SUT. We're using the combinations of AutoFixture, NSubstiture and…
Niels R.
  • 7,260
  • 5
  • 32
  • 44
0
votes
1 answer

How to use Nsubstitue for method with different arguments and return values?

For unit testing I am using NSubstitue to stub my repository method (say IRepo.GetOrder(orderId)) I am able to stub this for one specific argument like : IRepo RepoSub = Substitute.For(); Order ord = new…
rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
0
votes
1 answer

Convert Rhino Mock to NSubstitute

currently I am changing some implementation of Rhino Mock to NSubstitute. Now I have some difficulty here. could someone help me ? var provider = MockRepository.GenerateStub(); provider.Stub(e =>…
user7112196
  • 87
  • 2
  • 9
0
votes
1 answer

What is the equivalent argument matcher for a parameter like this - "Expression> predicate)" in NSubsitute?

I am trying to write a unit test for a class, and one of the dependency's method has a LINQ exresssion as a parameter passed into it. Now, I've mocked the dependency. But I am trying to tell this mock to return an object. Which I've done. But I…
user54287
  • 69
  • 9
0
votes
0 answers

UnexpectedArgumentMatcherException when calling Setup second time

I have these two tests (stripped to the bare bones to replicate the error): [TestFixture] public class CreditorMapperTests { private IAbcContext _AbcContext; [SetUp] public void Setup() { …
David Klempfner
  • 8,700
  • 20
  • 73
  • 153
0
votes
1 answer

NSubtitute: Mock private method

I am using NSubtitute, I want to test a method that calls a private methods (in same class), as the private method calls a SSRS to generate report, in real case, it works, but I don't want to call to a real report server in unit test, so I would…
Thien Long
  • 339
  • 2
  • 12
0
votes
1 answer

How to raise a delegate event using a ref parameter in NSubstitute 3.1?

I'm working on a C# project that uses a 3rd party library. This library defines a rather unusual delegate event using a ref parameter: event GetDataHandler OnGetData; public delegate bool GetDataHandler(string name, ref byte[] data); I'm trying to…
Baldewin
  • 1,613
  • 2
  • 16
  • 23
0
votes
1 answer

NSubstitute throws ArgumentSetWithIncompatibleValueException when out parameters gives back different derived type

I have a Key-value store keyValueDatabase. To request data IKeyValueResult keyValueDatabase.GetKeyValue(string id, out IKeyValue value) has to be called with the id for the requested value. The value is given back through the out parameter as an…
0
votes
1 answer

Unit Test with Nsubstitute allways return null with Lambda expression on Repository pattern

inside of the method which I'm evaluating in my Unit Test I want to return a mocked value which call my repository pattern, but always return null. I've tried with both options below but the behavior is the same (return…
gogoru
  • 376
  • 2
  • 19
0
votes
1 answer

How to spy method return value by NSubstitute

I want to spy return value of a mocked method of a mocked interface in NSubstitute. I get return value of Received function but it always returns null. public interface IFactory { object Create(); } public interface IMockable { object…
ali afshari
  • 62
  • 1
  • 7
0
votes
1 answer

How do I mock the .Where() extension of a mocked IQueryable using NSubstitite

so the code under test is: var query = _documentClient.CreateDocumentQuery(CollectionUri, GetFeedOptions()).AsQueryable(); foreach (var filter in filters) { query = query.Where(filter); } It throws an exception at query =…
Robert Green MBA
  • 1,834
  • 1
  • 22
  • 45
0
votes
0 answers

Checking a method had received a call with a list as a parameter

I have a service method that calls a repository method; for example: public async Task MyMethod(string param1, string param2) { var result = myRepository.Where(x => x.param1 == param1).ToList(); await myRepository.doStuff(result); I'm…
user11409100
0
votes
1 answer

How to check the parameters that were used in an NSubstitute mock

I know how to assert that a call to a method on a mock interface was done with specific arguments or with any arguments. However, I want to store the parameter that was used to call the function and run another assert on it, and I could not find…
asaf92
  • 1,557
  • 1
  • 19
  • 30
0
votes
1 answer

Protected virtual method of a mocked abstract class is not called

I am mocking an abstract class with NSubstitute and expect its protected virtual method to be called. public abstract class A { protected virtual bool ProtectedMethod() { return true; } public bool PublicMethod() { …
Vlad
  • 2,475
  • 21
  • 32