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

Can the MVC Controller method be mocked using the NSubstitute

I need to test and mock a method found inside a controller. Is is possible to mock a method inside the controller, without implementing an interface using NSubstitute framework. Here is my Controller page code. using System; using…
Kumaran Raj K
  • 365
  • 1
  • 11
4
votes
1 answer

Argument matching not working properly with NSubstitute

I have this code and I can't get httpClient to return the string response no matter what. HttpClient httpClient = Substitute.For(); httpClient.PostAsync("/login", Arg.Any()) …
Hristo Kolev
  • 1,486
  • 1
  • 16
  • 33
4
votes
1 answer

c# WCF Testing EntityDescriptor

I have a WCF client where I get data via OData. I want to unit test my client and already made an interface for the DataServiceContext: internal interface ODataServiceContext { DataServiceResponse SaveChanges(SaveChangesOptions options); …
scher
  • 1,813
  • 2
  • 18
  • 39
4
votes
1 answer

How to mock a record getting added to the DbContext

I have a method like this: private void CreateTaskFromModel(ForgotPasswordViewModel fpModel) { var message = _dbContext.Create(); message.MessageType = "TASK".PadLeft(10); message.Assigned_User_K =…
user6051680
4
votes
3 answers

Use Nsubstitute for Registering or Configuring for IOC container

I have a custom IOC container which accepts Interface and Concrete type as parameter to register. In my project I have registered the configuration as mentioned in below code. Can you help me someone how to register in unit testing project using…
Mathiyazhagan
  • 1,389
  • 3
  • 13
  • 37
4
votes
5 answers

How to test if function does not throw exception?

I have this function and test: public void SaveForWeb () { UpdateGameState(); try { PlayerPrefs.SetFloat(Helper.EXP_KEY, experience); PlayerPrefs.SetFloat(Helper.SCORE_KEY, score); // other properties that need to…
Vlad
  • 2,739
  • 7
  • 49
  • 100
4
votes
1 answer

How can I test for calls ignoring params arguments in NSubstitute?

I have code which looks like: eventPublisher.Publish(new SpecificEvent(stuff), EventStreams.Stream1, EventStreams.Stream2); which is calling a method defined as : Publish(T eventToPublish, params…
penguat
  • 1,337
  • 1
  • 13
  • 25
4
votes
1 answer

Extracting arguments from received call and assert on them

How can I do assertions on arguments from a received call? The below example does not work, because the action passed to Arg.Do() is never called. IEnumerable> receivedlArgs = null; provider.Received(1) …
bitbonk
  • 48,890
  • 37
  • 186
  • 278
4
votes
3 answers

How to use NSubstitute and/or AutoFixture to test a concrete class

I was hoping that by using AutoFixture and NSubstitue, I could use the best of what each have to provide. I have had some success using NSubstitute on its own, but I am completely confused on how to use it in combination with AutoFixture. My code…
Pranav Shah
  • 3,233
  • 3
  • 30
  • 47
4
votes
2 answers

TDD for Web API with NUnit, NSubtitute

I'm still confused with some TDD concepts and how to do it correctly. I'm trying to use it implement for a new project using Web API. I have read a lot about it, and some article suggest NUnit as a testing framework and NSubstitute to mock the…
Martin Valentino
  • 1,002
  • 2
  • 11
  • 26
4
votes
2 answers

Mock Autofac.IComponentContext with unit tests c#

I am trying to mock IComponentContext with NSubstitute as below: [TestClass()] public class SyncRepositoryFactoryTests { private IComponentContext _container; private SyncRepositoryFactory _factory; [TestInitialize] public void…
Leszek
  • 451
  • 5
  • 19
4
votes
0 answers

How to mock anonymous types properly?

I'm using a wrapper for HttpClient to be able to emulate http responses. I'm having a problem with those who are sending anonymous types. This is the setup response for my HttpClient class wrapper: var x = new {category = 1, products = "1"}; …
celerno
  • 1,367
  • 11
  • 30
4
votes
1 answer

Using Nsubstitute for mocking but getting an error

I am new to unit testing, so pardon me if I am unable to explain this question properly. I am reading a book "The art of Unit Testing 2nd Edition" and trying to implement unit testing in my project. I am currently stuck or confused when testing…
fais
  • 165
  • 2
  • 15
4
votes
1 answer

How to test that a method throws an exception on specific input

In the method below, when model.test is empty I want this method to throw an exception. public bool Create(Test model) { if (model.test == null) { throw new InvalidOperationException("nanana."); } try { return…
Timsen
  • 4,066
  • 10
  • 59
  • 117
4
votes
1 answer

Am I writing my unit tests correctly? NUnit + NSubstitute

I recently started learning how to write unit tests, and what part of the unit to test for functionality and what to mock out. I'm using NSubstitute as my mocking framework. My example basically calls a repo class, which then makes an WEB API web…
FaNIX
  • 1,888
  • 6
  • 33
  • 60