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

MVC4 Unit test NSubstitute Could not find a call to return from

I have a MVC4 web application I'm unit testing right now. It uses entity framework for the database portion. I'm using NSubstitute to mock the database. This code is basically copied and pasted from another site which works fine, so I hope I'm just…
Timothy
  • 1,198
  • 3
  • 10
  • 30
4
votes
1 answer

How to populate method's return values with AutoFixture

I would like to auto-generate a method's return values in a non-deterministic manner, i.e. with every call/test run to I expect a method to return random value. For the moment it returns always default values of the method calls: public…
Piotr Cierpich
  • 427
  • 2
  • 11
4
votes
3 answers

Mocking out expression with NSubstitute

I have a interface that contains the following method signature: TResult GetValue(object key, Expression> property) where T : class; Using Moq, I'm able to mock a specific call of this method like this: var repo = new…
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
4
votes
1 answer

How to fake an object in NSubstitute and ignore its method's internal implementation?

I'm new to NSubstitute and trying to fake an existing class named OrgDataWS. This class has a method named GetDataSet: public XmlElement GetDataSet(int token) { string perfLogMessage = string.Format("Org.GetDataSet: {0}",…
mkutkz
  • 99
  • 2
  • 8
4
votes
2 answers

Substitute a sealed class

I have a class A that expose a HttpRequestHeaders as a property. The class under test is B. B is using A. A is also a fake class that is used only for unit test. A inherit an interface that impose the definition of the HttpRequestHeaders…
mathk
  • 7,973
  • 6
  • 45
  • 74
4
votes
1 answer

NSubstitute: Arg.Do does not fulfill the list of called parameters

For the code bellow I get this assert failure and do not know why: Assert.AreEqual failed. Expected:<2>. Actual:<0>. public interface IA { void MethodA(B b); } public class A : IA { public void MethodA(B b) {/*no matter*/} } public class…
Petr Felzmann
  • 1,271
  • 4
  • 19
  • 39
3
votes
1 answer

NSubstitute Received() responding to multiple calls

I have an object that I've faked with NSubstitute that has a method on it that gets called twice. I'd like to verify that the method has actually been called twice (and only twice). I've poked around the docs and Google with no luck. Any help would…
BobC
  • 412
  • 1
  • 7
  • 20
3
votes
1 answer

Does NSubstitute have anything like It.IsAnyType in Moq now?

I am trying to use nsubstitute to test the ILogger in Microsoft.Extensions.Logging. Just as here says: Cannot test ILogger Received with NSubstitute , at that time, nsubstitute has no something like It.IsAnyType in Moq, I want to know whether…
AdvancingEnemy
  • 382
  • 3
  • 20
3
votes
1 answer

Function is called when I do not expect it to with NSubstitute

I am getting a behaviour I didnt expect from NSubstitute when setting up my mocks to call a function. A simplified version of the behavuiour is [Test] public void NSubstituteTest() { var mockedFoo = Substitute.For(); …
Jon
  • 561
  • 4
  • 14
3
votes
0 answers

Setting up NSubstitute Mock for functions that implement asp.net core IMemoryCache

I have a function that returns a status like this: public async Task UpdateWeight(string id, int weight) { var data = await service.GetData(id); if (data != null) { var user =…
3
votes
1 answer

How to verify that a substitute received no calls at all?

Using NSubstitute. For some tests I want to assert that a Substitute has received no calls whatsoever. I could use DidNotReceiveWithAnyArgs() for every method in the interface, but that is tedious and not as robust (if a new method is added to the…
E-Riz
  • 31,431
  • 9
  • 97
  • 134
3
votes
1 answer

Mocking user IP address in MVC unit test with NSubstitute

I have the following method in my HomeController. The purpose is to split users based on IP address to test different versions of the home page: [HttpGet] public ActionResult Index() { var userIpAddress =…
Jordan1993
  • 864
  • 1
  • 10
  • 28
3
votes
1 answer

NSubstitute ForPartsOf...When calls real method

I'm trying to substitute a method using ForPartsOf<...>() and then subst.Configure().MyMethod(...).Returns(...) or subst.When(x => x.MyMethod(..)).Returns(...), but in both cases the real MyMethod gets called. I was under the impression that both…
Kjell Rilbe
  • 1,331
  • 14
  • 39
3
votes
2 answers

How can I return IQueryable object from a mock object?

I am trying to tell a method GetAll() on a mocked object _portalUserRepositoryMock to return an object of type IQueryable. I know it is of this type because the method in the class to be tested returns this type. I've not been able to come…
3
votes
1 answer

How to verify AddSingleton with special type is received using NSubstitute framework

I want to mock IServiceCollection to check if AddSingleton is called with a specific Interface and concrete type using Nsubstite mocking library and xUnit. This is my unit test: [Fact] public checkIfServicesAddedTo_DI() { var…