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

NSubstitute use real instance of a class as substitute, except one method

Is there any built-in way in NSubstitute for mocking a class with its instance except for few methods? In example I want to preserve the whole functionality of the instance, but check if a method gets called with particular parameters. To do that…
UberFace
  • 445
  • 1
  • 4
  • 14
7
votes
3 answers

Does NSubstitute support the idea of Partial Mocks?

Does NSubstitute support the idea of Partial Mocks? http://nsubstitute.github.com/ http://www.ayende.com/wiki/Rhino+Mocks+Partial+Mocks.ashx
Simon
  • 33,714
  • 21
  • 133
  • 202
7
votes
2 answers

How do I mock DbContext using NSubstitute and then add/remove data

I need to mock EF's DbContext. I use the approach here and it works well. // mock a DbSet var mockDbSet = Substitute.For, IQueryable>(); var data = new…
h bob
  • 3,610
  • 3
  • 35
  • 51
7
votes
2 answers

How to mock protected method with NSubstitute

public static void Mock(out IProgram prog, out IJson json) { prog = Substitute.For(); IJson = Substitute.For(); prog.SaveProg(1, 1, 1, "Somthing", 1, DateTime.UtcNow, (DateTime.UtcNow +…
Karlen M
  • 101
  • 2
  • 5
7
votes
1 answer

nsubstitute received called with specific object argument

I have a class that looks something like this: public myArguments { public List argNames {get; set;} } In my test I'm doing this: var expectedArgNames = new…
SOfanatic
  • 5,523
  • 5
  • 36
  • 57
7
votes
1 answer

NSubstitute CouldNotSetReturnDueToNoLastCallException

I'm using NSubstitute to mock a class by PartsOf() method (I need some of the methods to work). It looks like this: var mock = Substitute.ForPartsOf(); mock.Start().Returns(void); A simple code almost like from the NSubstitute's…
andrew.fox
  • 7,435
  • 5
  • 52
  • 75
7
votes
1 answer

Mocking Task> with NSubstitute

I'm having issues trying to get NSubstitute to return an IEnumerable interface from a Task. The factory I'm mocking: public interface IWebApiFactory : IDisposable { GetOne(int id); Task> GetAll(); …
nik0lai
  • 2,585
  • 23
  • 37
7
votes
1 answer

How to mock object's indexer with private setter in NSubstitute?

I have an interface that is defined like the following public interface IFoo { object this[string key] { get; } } How can I mock this indexer using NSubstitute?
Andrew Stakhov
  • 1,105
  • 8
  • 26
7
votes
1 answer

Substitute read only property in partially mocked object

I'm testing an MVC controller which relies on a value returned from a read-only property on the base class. The getter for this property throws an exception when it is called as it is relies on a HttpContext (and other nasty stuff) which I would…
Ben Foster
  • 34,340
  • 40
  • 176
  • 285
7
votes
2 answers

Autofixture + NSubstitute: Freezing a mock?

I am trying to get access to a mocked (via Nsubstitute) class that was injected onto the constructor. I was using the following code var fixture = new Fixture() .Customize(new AutoNSubstituteCustomization()); var sut =…
Martin
  • 23,844
  • 55
  • 201
  • 327
6
votes
1 answer

How to Mock HttpClient using NSubstitute

I am trying to use NSubstitute to mock HttpClient. Here's the code: public static HttpClient GetHttpClient(bool isSucess = true, string methodType = "GET") { var mockIHttpMessageHandler =…
superninja
  • 3,114
  • 7
  • 30
  • 63
6
votes
1 answer

Does NUnit re-instantiate classes marked as TestFixtures between each test?

I have a TestFixture marked class which is Unit testing the functionality of a class named 'HideCurrentTitleBarMessageTask'. In this class I use a substitute to mock an interface in the [Setup] method, and in SOME tests in the test class I setup a…
Guy Joel McLean
  • 1,019
  • 4
  • 12
  • 34
6
votes
2 answers

How does NSubstitute .Returns() work?

How does the .Returns (this T value, ... ) extension method work under the hood? Specifically, how does .Returns know what method it is intended to configure just from the result of executing that method? Example: public interface ICalculator {…
coder958452
  • 145
  • 1
  • 6
6
votes
2 answers

How to set the return value for an IEnumerable class using NSubstitute

I have this domain: public class ADomainClass { public int Id { get; set; } } public interface IMyClass : IEnumerable { } public class MyClass : IMyClass { public IEnumerator GetEnumerator() { …
6
votes
1 answer

Nsubstitute Calls Method in When even though there is DoNotCallBase

I am partially mocking a class that has these two methods: public void EmitTo(string connectionId, ChatMessage message) { Clients.Client(connectionId).broadcastMessage(message.User.UserName, message.Message); } public virtual void…
Lomithrani
  • 2,033
  • 3
  • 18
  • 24