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

Avoid running function within Unit Test (NUnit)

Say I have a class: public class SomeClass { public Model _model; public SomeClass() { _model = new Model(); } public void Foo() { _model.DoSomethingHeavy(); } } And a Test: [TestFixture] public class SomeClassTest { …
aaronjbaptiste
  • 554
  • 4
  • 14
0
votes
1 answer

Mocking with NSubstitute, needing some clarity.

I am a bit confused about how to correctly mock an object. From what I have seen in the example on NSubstitute, this is the basic setup for an assert. My understanding is this is about testing the behavior of the method. My questions are as…
Mdukes00
  • 131
  • 1
  • 1
  • 8
0
votes
1 answer

Cannot mock a method for multiple invocation with Returns

I am trying to mock a method which takes two parameters int and out parameter of bool. I am able to set it up correctly for the first time invocation and it returns the right out value and return value. However, when trying to invoke the same method…
Yogesh
  • 2,198
  • 1
  • 19
  • 28
0
votes
1 answer

Unit Test NService.Send from an API Controller

I have an API Controller which publishes a command using NServiceBus. I am using NUnit and NSubstitute for testing. I want to test that certain properties from the model are populated on the command Here is my controller with a…
Bitmask
  • 918
  • 2
  • 11
  • 22
0
votes
2 answers

Mocking a DbConnection with NUnit/NSubstitute/AutoFixture and InsightDatabase

We're using Nunit, NSubstitute and AutoFixture to test a repository class that's built on top of Insight database... [TestFixture] public class CalculationResultsRepositoryTests { private IFixture _fixture; private IDbConnection…
James Law
  • 6,067
  • 4
  • 36
  • 49
0
votes
3 answers

NSubstitute: Received() does not check string parameter when string comes from resource

I use NSubstitute for my NUnit tests and try to check if a method is called with the right values. Until know everything works fine. I have to localize the texts and so use resource strings for this. Every unit test now fails, that tests for a…
scher
  • 1,813
  • 2
  • 18
  • 39
0
votes
2 answers

NSubstitue for IDBCommand and error CA2100

I want substituting IDBCommand with using NSubstitue. I must substitue field CommandText, and I did string settedCommandText=string.Empty; IDbCommand fakeCommand = Substitute.For(); command.CommandText =Arg.Do(x =>…
Alex
  • 1
  • 3
0
votes
1 answer

Is it safe to use the set of unit testing frameworks in the same project?

Visual Studio I use NUnit in my project with unit tests. But I need some tests to write with using JustMock and NSubstitute frameworks. I want to have one project with the tests for each my tested project. I.e. I don't want to have the individual…
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
0
votes
1 answer

Creating a Mock for HTTPMethods

In my controller I have a line of code like this string method = HttpContext.Request.HttpMethod; and based on if it is a GET or POST, method is doing different things that I want test. But first I should be able to mock this. We are using…
Bohn
  • 26,091
  • 61
  • 167
  • 254
0
votes
1 answer

Why unit test is failing although it contains all the values I am testing

This is the fail error I get on my xUnit with NSubstitute: Xunit.Sdk.ContainsException Assert.Contains() Failure Not found: Please send my Password. Name: Blake Lively Phone: 7021102502 I Lost my pass, Help In value: Blake Lively And this is what…
user6051680
0
votes
1 answer

How to undo `Arg.Do(() => {})` in NSubstitute

I've got this code: sub.Foo(Arg.Do(() => {})); How do I undo this? .ClearReceivedCalls() and .ClearReturnValues() seem to have no effect on removing this delegate
Andrew Bullock
  • 36,616
  • 34
  • 155
  • 231
0
votes
1 answer

Unit tests with FluentValidation. How to mock ValidationResult

I'm using TDD approach with xUnit 2, NSubstitute, AutoFixture, FluentAssertions for my unit tests. I want test my service method which using FluentValidation. Simple example: Validator: RuleSet("Nulls", () => { …
Nerf
  • 938
  • 1
  • 13
  • 30
0
votes
1 answer

Substitute With Property return and Collection with values with NSubstitute

This is a IValidationResult interface public interface IValidationResult { ICollection Errors { get; } bool IsValid { get; } IValidationResult Add(IValidationError error); IValidationResult Add(string…
Jedi31
  • 735
  • 1
  • 6
  • 22
0
votes
1 answer

NSubstitute returns first InlineData value for all tests

I am relatively new to testing. We use XUnit and NSubstitute as our testing frameworks and I am having trouble with what should be a simple test. I am using a Class library to interact with an external api and I need to be able to determine if the…
0
votes
1 answer

How to raise an event from Fake instance of class derived from interface

Code under test Public Class ObservableName Implements INotifyPropertyChanged Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Protected Sub RaisePropertyChanged(propertyName…
Fabio
  • 31,528
  • 4
  • 33
  • 72