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

Unit Testing Remove one Item in my Mock Database

I am on learning unit testing and have a question. I mocked my class ApplicationDbContext with my data. Now I would like to delete an item from my data. :( The first Test passed but the second failed it still shows me 20 items Can anyone help me? My…
0
votes
0 answers

Mocking Mongo ProjectionDefinition using NSubstitute

I am getting the following exception when trying to unit test a db call to a mongo database that uses ProjectionDefinition: "Value cannot be null.\r\nParameter name: projection". This is caused because during assignment for the MongoDb C# driver…
Nick Dichiaro
  • 330
  • 1
  • 2
  • 10
0
votes
2 answers

How to unit test a service call in xUnit and nSubstitute

I been trying to figure out how i can unit test service and so far have got nowhere. I am using xUnit and NSubstitute (as advised by friends), below is the simple test that i want to run (which fails currently). public class UnitTest1 { private…
Aeseir
  • 7,754
  • 10
  • 58
  • 107
0
votes
1 answer

NSubstitute ForPartsOf calling concrete implementation event when substituted

I have the following class: public class MyClass : IMyClass { public string MyFunc(string name) { if (string.IsNullOrWhiteSpace(name)) { throw new Exception("Blank Name"); } return…
Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0
votes
1 answer

CouldNotSetReturnDueToNoLastCallException when throwing exception

I have the following interface that I like to fake: public interface ElementSettings { ValueFormatter Formatter { get; } IEnumerable GetValidationRules(); } I would like to throw an exception, when the Formatter is gotten.…
scher
  • 1,813
  • 2
  • 18
  • 39
0
votes
1 answer

How register service type for Microsoft.OData.Edm.IEdmModel

I have implemented and ODataControllerExtensions class for my test cases. I opted to use NSubstitue for Mocking the DbContext and Entities. The problem is that when I try to get the ODataPath public static IQueryable InvokeForTest
Nkululeko
  • 31
  • 4
0
votes
1 answer

OneTimeSetup Failed Nsubstitute for Entity Framework and NUnit Test

I'm trying to create unit test for a project, which uses entity framework with (Microsoft.AspNet.Identity included). I create the substitute for my dbContext like this: var applicationDbContext = Substitute.For(); …
0
votes
1 answer

Not able see test cases in Text Explorer for the .net core 2 based Test project

I have created a test project based on .Net Core 2 and wrote some NUnit test cases. After installing necessary NuGet packages i.e. NUnit3TestAdapter, I was able to see all test cases in "Test Explorer" and able to execute those. Now, when I looked…
0
votes
0 answers

How to mock behavior of AutoMapper Mapping in Nsubstitute

Currently I have something like this :- var someclass1 = JsonConvert.Deserialize(RandomData); var someclass2 = AutoMapper.Mapper.Map(someclass1); Assert.Equal(someclass2.Count, 1); I know in…
Sharthak Ghosh
  • 576
  • 1
  • 9
  • 22
0
votes
0 answers

How to assign a mocked abstract class

I know this question has sort of been asked but I believe I have a variant over the other question/answers. I have this C# class: public class public CoreCycleTimer: MyAbstractClass { public CoreCycleTimer() { if ( !Initialised ) …
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
0
votes
1 answer

How to mock a property using NSubstitute while using a real implementation

I have some NHibernate repositories and I would like my SpecFlow tests to cover them. I have a repository for Staff like so: public class StaffRepository : NHibernateRepository, { public IEnumerable GetByStaffId(string…
ViqMontana
  • 5,090
  • 3
  • 19
  • 54
0
votes
0 answers

How to mock an injected class using NSubstitute?

I am using StructureMap for my dependency injection. I have the following repository: public class StaffRepository : NHibernateRepository, { public IEnumerable GetByStaffId(string staffId) { return…
0
votes
1 answer

How to resolve "named type" in NSubstitute?

I am using IUnityContainer for Registering the types for resolving later. I have registered the types as below: // Register a "named type" mapping container.RegisterType
0
votes
2 answers

How do I unit-test that a method correctly assigns a new value to another class

I am trying to unit-test the method Execute() in my InsertCashTransaction class. I want to test if it correctly assigns a new value to a User.Balance. You can see the two classes here InsertCashTransaction class public class…
Lumbaz
  • 3
  • 2
0
votes
1 answer

How to mock EF with NSubstitute

I have been tasked at work to create a test script that will (using entity framework) look-up a value in a table if existing. The code I have to work with has this constructor: public PostProductHelper( Func contextFactory…
Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179