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
10
votes
5 answers

NSubstitute - TestFixture 1 causes AmbiguousArgumentsException in TestFixture 2

I am writing C# unit tests using NUnit and NSubstitute. I am testing a class which will attempt to retrieve objects from a config provider implementing the following interface: public interface IConfigProvider { T GetConfig(int id); T…
Tarrenam
  • 382
  • 2
  • 11
10
votes
2 answers

How do I avoid using dynamic when mocking an Excel.worksheet?

I'm trying to mock an Excel spreadsheet using NSubstitute or other mocking framework and MSTest (Visual Studio 2010). I'm not sure if there's a better way than this--and this doesn't quite work for testing: Here's an example (this is all prototype…
tony722
  • 1,985
  • 2
  • 15
  • 14
9
votes
3 answers

How to provide mock values in a FeedResponse for CosmosSDK v3+?

I'm writing a data access layer for my application and trying to mock out the CosmosDB SDK dependency for unit testing. I am using NUnit with NSubstitute and have come across the issue where I am trying to mock the return values for…
Sin
  • 107
  • 1
  • 8
9
votes
1 answer

NSubstitute with delegates, checking received calls?

When checking received calls on an interface I can do this: void Main() { var logger = Substitute.For(); Test(logger); logger.Received().Log(Arg.Any()); } public void Test(ILogger logger) { …
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
9
votes
1 answer

NSubstitute - mock throwing an exception in method returning void

Using NSubstitute, how do you mock an exception being thrown in a method returning void? Let's say our method signature looks something like this: void Add(); Here's how NSubstitute docs say to mock throwing exceptions for void return types.…
Kavya Shetty
  • 185
  • 2
  • 14
9
votes
1 answer

Unable to cast object of type 'Castle.Proxies.XProxy' to type 'X' in WPF Designer

I've recently discovered the very useful design time attributes from Blend for a WPF component, which (among other things) allows you to set a DataContext only at design time. Awesome! Combined with the DesignInstance attribute, you can set a type…
Todd Bowles
  • 1,554
  • 15
  • 24
8
votes
1 answer

NSubstitute intercepting "setter" only property invocation

With NSubstitute, is there any way to capture the value you pass to a property setter? E.g. if I have the following interface: public interface IStudent { int Id { set; } string Name { set; } } The say I have a substitute created e.g: var…
bstack
  • 2,466
  • 3
  • 25
  • 38
8
votes
5 answers

NSubstitute Error UnexpectedArgumentMatcherException

I'm getting the following error: NSubstitute.Exceptions.UnexpectedArgumentMatcherException: 'Argument matchers (Arg.Is, Arg.Any) should only be used in place of member arguments. Do not use in a Returns() statement or anywhere else outside of…
user9530048
8
votes
1 answer

Return a new object for each Returns() in NSubstitute

I have created a substitute which mocks a web service interface for my unit testing which includes the following method definition: public Message Invoke(Message input) This method is called using: var reply =…
semantic_c0d3r
  • 491
  • 1
  • 4
  • 18
8
votes
1 answer

How can I mock HttpRequestMessage, specifically the CreateResponse?

How can I mock HttpRequestMessage, specifically the CreateResponse? var requestMessage = Substitute.For(); requestMessage.CreateResponse().ReturnsForAnyArgs( new HttpResponseMessage(HttpStatusCode.OK)); but I get the…
SteveC
  • 15,808
  • 23
  • 102
  • 173
8
votes
2 answers

NSubstitute ambiguous call when following documentation example (but with async method)

So I tried copying an example about exception throwing from the documentation, and added this to one of my methods: .Returns( x => { throw new Exception(); }); But this results in the following compiler error: Error CS0121 The call is ambiguous…
Kolichikov
  • 2,944
  • 31
  • 46
8
votes
2 answers

How Add test cookie to Request in C# unit test

How I can add test cookie to request so I can test my code from Unit test. Consider a code like this: public ActionResult Dashboard() { if (Request.Cookies["usercookie"] == null) { return RedirectToAction("Index"); } return…
Alma
  • 3,780
  • 11
  • 42
  • 78
8
votes
4 answers

NSubstitute cannot determine argument specifications to use

I use NUnit and NSubstitute for unit testing. I have the following: public interface IDataProvider { void Log(int tvmId, DateTime time, int source, int level, int eventCode, string message); } ... var fakeDataProvider =…
Drew
  • 169
  • 1
  • 1
  • 11
8
votes
1 answer

Mock result from Func with NSubstitute

I'm trying to use NSubstitute to mock the return value from a Substitute, but I cannot get the substitute to return the correct value because the method signature is using a Func. I've seen these questions, but cannot make it work with my…
smoksnes
  • 10,509
  • 4
  • 49
  • 74
8
votes
3 answers

How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture?

I am using the [AutoNSubstituteData] attribute, which was posted here: AutoFixture, xUnit.net, and Auto Mocking I would like to combine this with the [PropertyData("")] attribute from xunit extensions. This is my test: public static…
mrt181
  • 5,080
  • 8
  • 66
  • 86
1 2
3
41 42