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

Mocking IFlurl library methods using NSubstitute is throwing null reference exception

I am using flurl and I am trying to unit test the code below: public class MyRestClient { public async Task Request(IFlurlRequest flurlRequest) { try { return await flurlRequest …
boywonder
  • 116
  • 2
  • 11
0
votes
1 answer

Is it good practice to use NSubstitute(or other testing frameworks that allow mocking) in the code(not in the tests)?

I faced with usage of NSubstitute inside business logic(outside of test classed): var extension = Substitute.For(); I get used to utilization of NSubstitute inside test classes, when you need to mock some class(interface). But using…
Anton
  • 9,682
  • 11
  • 38
  • 68
0
votes
1 answer

NSubstitute - Argument matcher problem with ref / out

I wanna create a mock for my MySubClass. But, it has an argument ref in one of its methods. The argument ref is an object of type MyReference. The problem is: I can't use the same 'reference' ref inside my class, so the conditions aren't being…
0
votes
1 answer

Raising an event with NSubstitute in VB.NET

I'm having trouble raising an event in a unit test using VB.NET and NSubstitute. The interface being mocked defines an event: Event BlockOfVehiclesProcessed(source As Object, stats As ProcessingStats) The class under test registers a handler for…
Mike Woinoski
  • 3,123
  • 1
  • 16
  • 15
0
votes
0 answers

How to mock HttpContext session in asp.net core 2.1

Controller.cs public async Task BookingRequest(int cid) { string[] requestParams = Request.QueryString.ToString().Split("&"); BookingVM objSessionBookingVM = new BookingVM(); var value =…
Suvethan Nantha
  • 2,404
  • 16
  • 28
0
votes
2 answers

Memory leaks using NSubstitute interface in a loop?

If I run the following program I see the free memory rapidly decrease to zero in Windows Task manager. Is it forbidden to use NSubstitute in loops? using System; using NSubstitute; using System.Threading; namespace NSubstituteMemoryLeaks { …
0
votes
1 answer

NSubstitute with generics

I am trying to write a unit test for an existing method. One of the things this method does is call a web service with IRestClient. This line of code is: var jobs = Client.Get>(request).Data; where Client is an…
0
votes
1 answer

Nsubstitute - Raise event of mocked object when handler uses generics

We mock an interface which has an event on it like so: public interface IThing { event EventHandler> OnMessage; } Using NSubstitue we make a mock of the interface and try to raise the event using NSubstitute…
FBryant87
  • 4,273
  • 2
  • 44
  • 72
0
votes
0 answers

Cookies are not available in Class library while doing unit testing

I am doing unit testing for my mvc application. It includes many class library. I have mocked the http context in unit testing project using NSubstitute and created the cookie. But it is not available in class library while debug the source. How can…
Melody
  • 1,203
  • 2
  • 17
  • 28
0
votes
1 answer

NSubstitute not responding to Received() assertation

I have a test which mocks a non-interface object with a constructor that takes parameters: [Test] public void Test() { var myObject = Substitute.For("param1", "param2"); _sut.DoSomething(myObject); …
FBryant87
  • 4,273
  • 2
  • 44
  • 72
0
votes
1 answer

Unit testing middleware with NUnit and NSubstitute

I've written a bit of middleware in an ASP.NET Core site and I'm trying to unit test it, mainly by following this guide that uses Moq. My problem is finding an NUnit/NSubstitute equivalent for new DefaultHttpContext(). Substituting HttpContext will…
Red
  • 3,030
  • 3
  • 22
  • 39
0
votes
2 answers

NSubstitute out arrays, with ambiguous parameters?

NSubstitute is complaining that my arguments are ambiguous, however as far as I'm aware they are fully spec'd. I'd add more details but I've already boiled it down to this small example. (Edit: now even smaller, removed out parameter, but not the…
Ryan Leach
  • 4,262
  • 5
  • 34
  • 71
0
votes
0 answers

Duplicate type name within an assembly (with NSubstitute)

Since this morning, our CI build failed due to surprising "System.ArgumentException : Duplicate type name within an assembly." exceptions during our tests execution. (note: our tests project built in .NET (4.6) using references to NUnit (3.9.0.0)…
yi.han
  • 369
  • 4
  • 8
0
votes
1 answer

How do I unittest when having an automapper map to existing object and using nsubstitute

I am using Automapper 6.2.1 and by that removed all statics and instead I am injecting IMapper. I am using NSubstitute for mocking. I have a bit of code where I map two existing objects. public class Person1 { public string Value1 { get;…
Per
  • 1,393
  • 16
  • 28
0
votes
2 answers

NSubstitute checking complex arguments inside a Received() check

Looking here, I'm led to believe that what I'm trying to do is possible. However, I seem to be getting a failing test, when I believe it should pass. Here's my test code: // Arrange dbOperations = Substitute.For(); myClass = new…
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269