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

C# collection with NSubstitue proxy objects overwrite Equals

I had a problem with using .NET collections with Nsubstitue objects. I have a base class where I implement Equals(object), CompareTo function In the test I create two exact Nsubstitue object proxy for this base class. After I put the object in the…
books
  • 3
  • 1
0
votes
2 answers

Test method with multiple method `Returns` possibilities

I want to execute one Test method several (3) times with different behaviour of a mocked object. So I can avoid writing multiple Test methods with mocked objects for each. Code as example: _mockObj.MockedMethod(Arg.Any(),…
Ozkan
  • 3,880
  • 9
  • 47
  • 78
0
votes
1 answer

How can I mock a singleton class with NSubstitute?

I have a Singleton class, something like this : public class XConnector : IXConnector { private static readonly Lazy instance = new Lazy(() => new XConnector()); public…
user5447154
0
votes
1 answer

Can NSubstitute mock a return value based on an interface arg rather than concrete type?

I have an entity, and a validator: public class Customer : IEntity { /* ... */ } public class CustomerValidator : IValidator { /* ... */ } I want to mock the validator factory: public interface IValidatorFactory { IValidator
grokky
  • 8,537
  • 20
  • 62
  • 96
0
votes
2 answers

NSubstitute, try catch is not working on async method configured to throw an exception

I'm using NSubstitute for mocking and faking. I'm working with EF6 and would like to setup the SaveChangesAsync-Method of the database context to throw an exception: context.SaveChangesAsync().Throws(new DbUpdateException("",…
yan.kun
  • 6,820
  • 2
  • 29
  • 38
0
votes
1 answer

NSubstitute IEnumerable Returns

I've a class that has a IEnumerable property: interface ICore { IEnumerable enumerable { get; } } I need to substitute the return values of this IEnumerable. I've rtied with Returns, nevertheless, I'm getting a message…
Jordi
  • 20,868
  • 39
  • 149
  • 333
0
votes
1 answer

Is partial mocking of a security a good practice?

I am introducing automatic testing using NUnit, NSubstitute for a project which uses Ninject and generic repositories. For regression testing, I am replacing generic repositories with in memory ones to prevent working with a database. Also, to test…
0
votes
0 answers

NullReferenceException on Received() in NSubstitute

I have the following code I want to verify with NSubsitute: public PkiBodyBuilder AddExtension( DerObjectIdentifier oid, bool critical, Asn1Encodable value) { this.extensionsGenerator.AddExtension(oid,…
Frank
  • 2,036
  • 1
  • 20
  • 32
0
votes
1 answer

How to return object in NSubstitute with Substitute parameters

I am new to NSubstitute and have previously worked with Moq. I want to call a function with any arguments and return an object that I create with any constructor args except one that I want to set. In Moq i can write this: new…
EdwardB
  • 76
  • 1
  • 6
0
votes
1 answer

partial mocking of methods of concrete class

BTPhysicalAccount testerInstance = new BTPhysicalAccount(IvirtualAccounts, BTmarketIn, int); I want to test a method of this class. that BTmarketIn is a concrete class and a method in this class is used in that method under test. So I needed to…
Usama Aslam
  • 437
  • 7
  • 18
0
votes
1 answer

How to test that the list Count is a certain value?

I am trying to test one of my methods by seeing how many results are being passed into my Save method. The pertinent line is: await paymentSampleRepository.Received() .SaveSamplesAsync(Arg.Do>(x => …
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
0
votes
2 answers

Accessing non-static Fields without creating an object

I am inexperience with c# and I would like to ask if there is any way to access non-static fields without creating an object? I am unit testing a program(so I can't change the way the class is written) and my aim is to get the initialised values of…
Angelo Charl
  • 347
  • 4
  • 15
0
votes
0 answers

Unit tests with new class as parametr

I have problem with mocking some method: Interface method: bool IsUserAuthorizedToAction(AuthorizationContextData contextData, AuthorizedActionType actionType); How is called: _userInformation.IsUserAuthorizedToAction(new…
Nerf
  • 938
  • 1
  • 13
  • 30
0
votes
2 answers

NSubstitution for methods in c#?

I am trying to use NSubstitution in my C# Unit testing program. I am trying to do a substitution on a method called inside of a mehtod. here is an example: public class operation { public int addition(int a, int b) { return (a + b); } }…
Angelo Charl
  • 347
  • 4
  • 15
0
votes
1 answer

NSubstitute if-else condition

Good day! I need your help, I have next tests: [SetUp] public void SetUp() { controller = Substitute.For(); view = Substitute.For(); presenter = new…
Crispried
  • 99
  • 3
  • 11