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

Raise.Event> : Cannot raise event with the provided arguments

When I run the unit test below, it fails and returns this message: System.ArgumentException : Cannot raise event with the provided arguments. Use Raise.Event(CWaveform[]) to raise this event. [Test] public void…
blueshift
  • 831
  • 2
  • 12
  • 24
0
votes
1 answer

ForPartsOf received calls not showing up

For testing purposes I want to run the real implementation of a class but see the functions that where called on it after its execution. If I understood that documentation correctly that is what the ForPartsOf is for. However after using it, it…
Nick Otten
  • 702
  • 7
  • 17
0
votes
1 answer

NSubstitue Received() does not work when I run multible test in row, but if I run the test individually they work

I have multiple unit tests, where I do check if a method was called. I use the NSubstitute mocking libraries to check rather a method was called with the help of the "Received()" method, just like…
Whatever
  • 77
  • 1
  • 12
0
votes
1 answer

Force NSubstitute to throw exceptions for every call

I'm using NSubsitute to mock a class that my method under test uses. I want to ensure that my method does not throw exceptions that are caused by its dependencies. Is there a way to force a mocks to throw an exception, no matter which of its methods…
al-bex
  • 666
  • 1
  • 9
  • 24
0
votes
1 answer

Overriding protected abstract method in NSubstitute

I want to create a spy using NSubstitute, but can't find out how after a lot of googling and reading the docs. The idea would be to have the substitute with an overriden method that I could use to monitor received calls, etc. as usual with mocking.…
heltonbiker
  • 26,657
  • 28
  • 137
  • 252
0
votes
1 answer

How to configure NSubstitute NOT call internal virtual method

I want unit test this class public class Email { public async virtual Task Send( ) { //code await Save(); } internal virtual async Task Save( ) { } } and this unit test code var email =…
tsohtan
  • 830
  • 1
  • 13
  • 33
0
votes
2 answers

Unit test (NUnit, Nsubstitute) ASP Core Service with MongoDB

I got a simple application that calls a MongoDB collection, and it does various things with it. I want to unit test my service layer using NUnit, Nsubstitute, but I have no idea how to mock a data collection that my service layer consumes. Here is…
Aeseir
  • 7,754
  • 10
  • 58
  • 107
0
votes
1 answer

NSubstitute: Trouble mocking a syntatic sugar getter method associated with a member variable with No corresponding setter

For my .NET C# application, I'm using a third-party e-faxing software named efaxdeveloper.com I needed to mock efaxdeveloper.com's software OutboundResponse object. Please keep in mind that since it's 3rd party, I obviously can Not modify the…
crazyTech
  • 1,379
  • 3
  • 32
  • 67
0
votes
0 answers

Get functions return value "mid-run" in .net?

I am trying to create a system test for an old code base where there is no interfaces and most functions are private. The application gets data from another system, transforms it to a "better format" and saves it to a cache. I want to read the…
Nings
  • 355
  • 1
  • 2
  • 12
0
votes
1 answer

NSubstitute raising an event on a mocked subclass

What I have is a wrapper around a class from an external library (this case it's WebSocketSharp), the wrapper class reacts on certain events like when a connection was established etc. To test this wrapper class I've mocked the WebSocket class and…
Nico
  • 559
  • 4
  • 22
0
votes
1 answer

mock function with variable number of aguments using NSubstitute

I have an interface with the following method: bool ProcessActions(int actionTypeId, out List ints, params object[] actionParameters); Now how can mock this method to return a value using NSubstitute? Here is what I've tried: …
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
0
votes
0 answers

Mocking the httprequest with an manually defined accepttype or null

I am currently trying to unittest a this bool hasImageUrl = HttpContext.Current.Request.AcceptTypes?.Any(x => x.ToLower().StartsWith("image/")) ?? false; Which require me to set the accepttype, since when I mock it by HttpRequest httpRequest =…
anita
  • 193
  • 10
0
votes
0 answers

NSubstitute ReceivedCallsException error when run calls in a sequence

When I run below tests in below sequence ((i.e. Test1 and then Test2) it throws the mentioned error. But it works fine if it runs opposite sequence. (i.e. Test2 and then Test1) [Fact] public async Task Test1() { var body =…
Shabar
  • 2,617
  • 11
  • 57
  • 98
0
votes
2 answers

How do I use NSubstitute to mock ILogger on a constructor

I have the following concrete controller constructor: public class AuthenticationController : ControllerBase { private readonly IUserRepository _userRepository; private readonly ILogger _logger; private…
bilpor
  • 3,467
  • 6
  • 32
  • 77
0
votes
1 answer

Multiple NSubstitute call configurations on method accessing different properties on a reference type parameter (to avoid NullReferenceException)

I have the following test code using NSubstitute: [TestMethod] public void Test() { var foo = Substitute.For(); foo.Foo(Arg.Is(b => !b.X)).Returns(0); // Line 1 foo.Foo(Arg.Is(b => b.X)).Returns(1); // Line 2 } public…
Neo
  • 4,145
  • 6
  • 53
  • 76