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

NSubstitute test for void method

I would like to verify that the parameter idStatus is assigned to the myObj instance. How can I do this with NSubstitute? I thought that if I somehow could verify the parameter to UpdateObject this would be a good test of the method, but not sure…
Kman
  • 4,809
  • 7
  • 38
  • 62
0
votes
2 answers

How to test a public method that changes a variable with defined getter

I have a game that has coins. There is an Add() method that changes the coins. I want to test if it adds correctly. public class CoinsService { public ReactiveProperty Coins { get { return State.SaveGame.Coins; } } public int Add(int…
Lee Jaedong
  • 95
  • 1
  • 8
0
votes
2 answers

NSubstitute using FOrPartsOf to configure void method to do nothing

I have this simple example where I would like to test if a method is being invoked on the same class as the calling method: public class MyClass { public void SomeMethod() { SomeSubMethod(); } …
Peter Poulsen
  • 440
  • 4
  • 11
0
votes
0 answers

How to mock an operator overloading with NSubstitute?

I'm testing a class ClassA that takes an instance of a class ClassB in its constructor. In my test of ClassA, I mock ClassB with NSubstitue. However, ClassB overload the * operator. How can I set the Result of the * operation for my ClassB mockup?
François
  • 3,164
  • 25
  • 58
0
votes
2 answers

Mock static class

I'm trying to mock a static class and static method in C# using NSubstitute. I obviously don't want to Generate an excel file for every unit test. So I definitely want to mock. But since this is a static class, I'm unable to mock as this is a static…
VKh
  • 151
  • 2
  • 10
0
votes
1 answer

How convert from Moq to NSubstitute that code?

Moq: var someService = new Mock(); var data = new ...; someService.Setup(x => x.UpdateAsync(It.IsAny>())) .Callback>((func) => { func(data); }) …
gen32
  • 11
  • 3
0
votes
1 answer

How to unit test that tasks are run synchronously

In my code I have a method such as: void PerformWork(List items) { HostingEnvironment.QueueBackgroundWorkItem(async cancellationToken => { foreach (var item in items) { await…
Herman
  • 51
  • 1
  • 6
0
votes
2 answers

Mocking domain entity class' properties in unit tests: methods, abstract classes or virtual properties?

I have a class that represents a domain entity and this class does not implement any interfaces. Let's consider something as simple as: public class DomainEntity { public DomainEntity(string name) { Name = name; } public string Name { get;…
Philip P.
  • 1,162
  • 1
  • 6
  • 15
0
votes
1 answer

NBuilder and DbContext invalid cast issue

I am really new to NBuilder, but it looks awesome so I thought I would have a go. I have a DatabaseContext which just inherits from DbContext like this: public class DatabaseContext : DbContext Now, I have created a service that queries the…
r3plica
  • 13,017
  • 23
  • 128
  • 290
0
votes
0 answers

NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching error while unit testing

I am getting a below error while unit testing the piece of code NSubstitute.Exceptions.ReceivedCallsException: Expected to receive a call matching error while unit testing NSubstitute.Exceptions.ReceivedCallsException HResult=0x80131500 …
kudlatiger
  • 3,028
  • 8
  • 48
  • 98
0
votes
0 answers

How to test if an object of the same class received an event

I have two Players and one Player to damage the other. Because I don't want to use a Singleton or reference my Game Manager class and referencing a Player from within in a Player is impossible, i don't have a reference to the Enemy. If some one…
0
votes
2 answers

Is there any way to mock property without setter using nsubstitute?

I have some code, like: class MyClass { private int _myField; public int MyField => _myField; } How can I mock up MyField using nsubstitute? Or it's impossible?
Shadr
  • 214
  • 1
  • 12
0
votes
1 answer

Mock a property with a private setter using NSubstitute

I have a class public class MyClass { public int MyProperty1 { get; private set; } public int MyProperty2 { get; private set; } public MyClass(){} public MyClass(int myProperty2) => MyProperty2 = myProperty2; } in my unit tests i need…
Chitova263
  • 719
  • 4
  • 13
0
votes
1 answer

Match a func delegate in argument matcher in nsubstitute Received method

I am trying to check if a method is called a specific number of times on a mock instance of a class. The problem is the method has a func delegate and that is not matching. I have the following scenario: public interface ISomeService: IService { …
Sunil Kumar
  • 390
  • 1
  • 7
  • 25
0
votes
1 answer

Mocked method returning null when called from a different project

I have this test class: using NSubstitute; using NUnit.Framework; using System; using System.Linq.Expressions; namespace MyTests { public class Tests { [Test] public void Test() { var companyBL =…
David Klempfner
  • 8,700
  • 20
  • 73
  • 153