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

NSubstitute virtual getter returning substitution using ForPartsOf throws exception

I am trying to mock one property of an object There is a similar question: Returning the result of a method that returns another substitute throws an exception in NSubstitute But the accepted answer is not working for me. void Main() { var obj =…
Thymine
  • 8,775
  • 2
  • 35
  • 47
5
votes
2 answers

Internal properties are not handled by NSubstitute

Suppose, that I've got a following class: public abstract class Test { internal abstract int Prop { get; } } Now, I try to make a mock using NSubstitute: var mock = Substitute.For(); But that fails: Method 'get_Prop' in…
Spook
  • 25,318
  • 18
  • 90
  • 167
5
votes
1 answer

NSubstitute: Mocking the request, response object inside a MVC/Web Api Controller?

I am trying to find how to mock both the Request and Response objects that are available inside a controller from MVC / Web Api. Is this possible, I am not injecting the Request and Response objects, these are available because the controllers…
Martin
  • 23,844
  • 55
  • 201
  • 327
5
votes
1 answer

Verifying a call parameter via a callback in NSubstitute

I have a failing test in NSubstitute because a parameter passed in to a substituted call does not match. Here is the relevant code that is being tested: // Arrange PermissionsProviderSub = Substitute.For(); MenuDataProviderSub…
levelnis
  • 7,665
  • 6
  • 37
  • 61
5
votes
2 answers

NSubstitute mock generic method

I have the following method signature in an interface: public interface ISettingsUtil { T GetConfig(string setting, dynamic settings); } Which I have attempted to mock: var settingsUtil = Substitute.For(); var maxImageSize =…
Anders
  • 15,227
  • 5
  • 32
  • 42
4
votes
2 answers

Two dimensional object array return type - NSubstitute

I get a cast exception System.InvalidCastException : Unable to cast object of type 'System.Object[]' to type 'System.Object[,]'. at Castle.Proxies.ITestProxy.Get2DArray() at Scratch.TestFixture.Get2DArray() in TestTest.cs: line 17 from from the…
Myles McDonnell
  • 12,943
  • 17
  • 66
  • 116
4
votes
1 answer

How to unit test structured logging calls using NSubstitute?

I have a method which has a line as follows: _logger.LogError(exception, $"Encountered {exception.GetType().Name}. Unable to verify user with id {user.UserId}"); This has a corresponding unit test with the following assertion: var logger =…
asten_ark
  • 47
  • 4
4
votes
1 answer

Mocking a function with optional parameter with fixed parameter using Nsubstitute in C# returns null

I have been trying to mock a function that included optional parameters with fixed parameters but every time I am getting a null value here is my function defined in interface which I want to mock: List GetEntitiesByIDs(List ids, bool…
Arnav Garg
  • 51
  • 3
4
votes
1 answer

How to mock 'out' parameter?

I have downloaded the latest NSubstitute release, 1.1.0, May 21, 2011. Prior to this release, it seems that NSub did not support out parameters. It appears that some work has been done to provide support through an intermediate release: NSub Google…
IAbstract
  • 19,551
  • 15
  • 98
  • 146
4
votes
3 answers

How to mock Entity Framework's FromSqlRaw method?

I am writing a Unit Test and need to mock Entity Framework's .FromSqlRaw method. When the method is executed in the class under test, it throws following exception: System.InvalidOperationException: There is no method 'FromSqlOnQueryable' on…
4
votes
2 answers

NSubstitute Mock static class and static method

I'm new at unit testing, and I'm trying to mock a static method in a static class. I already read that you can't do that, but I was looking a way to work around that. I can't modify the code, and making the same function without being static is not…
4
votes
1 answer

Why can most of the mock frameworks in .NET (Core) not mock static and private methods?

My question is about the technical reason for this limitation, not about how to fix it. Why do some frameworks like Telerik JustMock and Typemock Isolator support these features, but we can not have these in Moq or FakeItEasy or NSubstitute,…
HamedFathi
  • 3,667
  • 5
  • 32
  • 72
4
votes
1 answer

How to raise an event with NSubstitute with EventHandler?

I have an interface which defines multiple events, some with delegate type EventHandler with T for example . Objects implementing this interface are used in another class, a simple arrangement would look like this: public interface…
lars k.
  • 562
  • 4
  • 18
4
votes
1 answer

Override Autofixture customization setup

I've a class with several services injected in its constructor. I'm using Autofixture with xUnit.net and NSubstitute, and created an attribute to setup the global customization. public class AutoDbDataAttribute : AutoDataAttribute { public…
adelb
  • 791
  • 7
  • 26
4
votes
0 answers

Nsubsitute - Mock db context which is invoked inside query handler

Is there any way to mock dbcontext in situation like this? Im using cqrs. public class QueryHandler1 : IQueryHandler { private readonly IDbInvoker invoker; public QueryHandler1 (IDbInvoker invoker) { …
TjDillashaw
  • 787
  • 1
  • 12
  • 29