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

How to test set method on nsubstitute mock

I am completely re-writing this now that I am sitting in front of my project. I am still having issue with understanding how to write the test correctly. Here is the class I am trying to test: namespace Snowball.Controllers { public class…
ShaffDaddy
  • 63
  • 1
  • 9
0
votes
1 answer

NSubstitute creating two instances of substituted instance

I've got a base ApiController for my controllers to inherit: public BaseApiController(ILogger logger) : ApiController { private readonly ILogger _logger; public BaseApiController(ILogger logger) { _logger =…
James Law
  • 6,067
  • 4
  • 36
  • 49
0
votes
1 answer

Test entities disappear after enumeration in Entity Framework 6 unit test

I'm using NSubstitute to test an application using Entity Framework 6. I used this article as the base for my setup: https://msdn.microsoft.com/en-us/library/dn314429.aspx Whenever my code enumerates an entity set twice, all the entities disappear.…
Vilém Procházka
  • 1,060
  • 2
  • 17
  • 28
0
votes
2 answers

How to test a function that has only switch logic?

I have a code of my Save() function, it has only switch statement inside. So basically it saves based on the platform chosen. However I already have tests for UpdateGameState(), SaveForWeb() and SaveForX86() functions. Since Unit Testing rules say,…
Vlad
  • 2,739
  • 7
  • 49
  • 100
0
votes
1 answer

How to mock a function call in the method I am testing - currently using NSubsitute

I am new to trying to mock things in unit tests... Example Code simplified for posting: namespace MockInvestigate.Monitor { internal interface IAgentRepo { Dictionary GetAgentAppSettings(string moduleName); } …
Vle Jo
  • 3
  • 4
0
votes
1 answer

NSubstitute auto-mocking certain types only

I've noticed that NSubstitute auto-mocks the following types: Array IObservable String Task Although I know this happens, I can't find the reason for picking these types. Why not leave String null? Or include collections instead of just Array? For…
m.edmondson
  • 30,382
  • 27
  • 123
  • 206
0
votes
2 answers

How can I make Ninject kernel return NSubstitute mock object?

I have static method in static class which injects my dependencies: public static void InjectDependency(NinjectModule module); I have static property to get instance of each object: public static IKernel AppKernel {get;set;} If I'd like to…
klutch1991
  • 229
  • 2
  • 16
0
votes
1 answer

How to set value to a local variable of a class using NSubstitute in TestProject?

I need to mock with NSubstitute and need to set local variable command of a class LoanCreateHandler to mock data with it's parameter Z. I have code like give below: public class ClassA { public string Prop1 { get; set; } public string Prop2 {…
Rohit Sonaje
  • 522
  • 4
  • 14
0
votes
1 answer

How can I redirect arguments using NSubstitute

I am in the process of converting some test objects from RhinoMocks to NSubstitute and am having trouble setting up my mock repository. See the following unit test code below: [SetUp] public void SetUp() { _converter =…
Cameron Stubber
  • 301
  • 3
  • 14
0
votes
1 answer

EF 6 fake db context, can't find the entity

I'm in the middle of covering some of our service classes with unit tests and I have managed to isolate/fake the dbcontext using NSubstitute (following this guide). I have some tests done and working, and things seemed to be alright, but now I can't…
sara
  • 3,521
  • 14
  • 34
0
votes
1 answer

NSubstitute does not print out NUnit assertion

I have just started using NSubstitute. I mostly worked with Moq, and this is what i was doing: // In my unit test on menter code herey mock: HasLogMessage(Is.EqualTo("expected value")); private void HasLogMessage(EqualConstraint s) { …
Darius
  • 1,150
  • 10
  • 12
0
votes
1 answer

Am I creating fakes correctly with nsubstitue?

I got an assignment to learn how you use an isolation framework. And I was wondering if I am creating fakes the appropriative way with nsubstitute. Here is a sequence diagram of how the application shoud look like . I have then made unit tests and…
Sumsar1812
  • 616
  • 1
  • 9
  • 32
0
votes
0 answers

NUnit NSubstitute not expecting exception

I am pretty new to unit testing and NUnit, my case is that a test expecting a simple exception stopped working when I changed from the Ninject NSubstitute mocking kernel adapter to plain NSubstitute for mocking Returns purposes. private…
Miguel A. Arilla
  • 5,058
  • 1
  • 14
  • 27
0
votes
1 answer

NSubstitute - setting a property of the calling object

I have a in issue where I would like to swap out Moq for NSubstitute entirely. In most cases, this is extremely straightforward, however I have run across a fairly specialized issue. Here's the Moq code. _registrationCommandHandler.Setup(c =>…
Rich Bryant
  • 865
  • 10
  • 27
0
votes
1 answer

UnitTesting PatIndex

when unit testing PatIndex of SqlFunctions with a mocked IQueryable object I'm getting the following error: "This function can only be invoked from LINQ to Entities." Here is how I initialize my mock repositories: protected void…
DAG
  • 2,460
  • 5
  • 33
  • 61