NSubstitute | A friendly substitute for .NET mocking libraries | Unit Testing library
kandi X-RAY | NSubstitute Summary
kandi X-RAY | NSubstitute Summary
A friendly substitute for .NET mocking libraries.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of NSubstitute
NSubstitute Key Features
NSubstitute Examples and Code Snippets
Community Discussions
Trending Discussions on NSubstitute
QUESTION
I'm trying to create an AutoFixture.ISpecimenBuilder
for this class:
ANSWER
Answered 2022-Mar-25 at 12:02Activator.CreateInstance()
only looks for public constructors by default. If you want to look for other scopes of constructor, you need to use the overload with BindingFlags
.
The flags needed are:
QUESTION
My test requires that I have different counts of objects in an IEnumerable property of the main entity collection. I have been searching for documentation about this but can't find anything. Here is a sample of what I mean (note that the base entity is created using AutoNSubstituteCustomization
)
ANSWER
Answered 2022-Jan-21 at 17:31It seems that the issue you're having is not related to AutoFixture but rather with NSubstitute.
Since ITransaction
is an interface AutoFixture will delegate the task of creating and instance to the mocking library. In your case that's NSubstitute.
Since your interface only declares getters but no setters, NSubstitute will generate a dynamic proxy, for your interface, that as will as well not have any public setters. This is why AutoFixture is unable to set the values of your properties.
So if you want to continue using the mock, you'll have to either specify a public setter in your interface or tell AutoFixture how to set the values using the NSubstitute API. Unfortunately you'll be able to implement the second option only by implementing an ISpecimenBuilder
factory for your interface and then play with reflection.
Another way, which is what I recommend, is to relay the setup of your interface to a fake implementation, which you'll create by hand and which will have the public setters. Then you'll instruct AutoFixture to relay all requests to the interface to your fake class.
QUESTION
I am using NUnit, NSubstitute, and Visual Studio 2022 for testing an ASP.NET Core 6 Service. I'm seeing some results that I don't understand. Here is a screen capture, showing the code and the coverage results:
The color coding is saying that line 71 (throwing the new exception) is covered, but the if on line 69 is only partially covered. How is this possible? To get to line 71, both of the conditions on either side of the && operator must be executed.
...ANSWER
Answered 2022-Mar-11 at 20:17Answering my own question...
The issue was that I didn't have a test case where customer.Dead.HasValue == false, or said another way, where customer.Dead == null. The database and entity allow for a null value here, but there was no unit test of null.
QUESTION
I have the following code in my class under testing:
...ANSWER
Answered 2021-Nov-10 at 12:09I have the following code in my class under testing:
devices = ContainerLocator.Container.Resolve();
That's the reason why you don't inject the container. In fact, you avoid using the container at all except during the registration phase and the initial call to Resolve
.
So my question is how could I make a substitution for
ContainerLocator.Container
?
The answer's: you don't
What you should do instead is to let the container do the resolving work by injecting what you actually want, not the almighty container.
QUESTION
I want to test a class that depends on another class with the virtual method.
...ANSWER
Answered 2021-Nov-10 at 00:23Because DepClass
is neither an interface nor an abstract type, by default, AutoFixture will not rely on a mocking framework to create the instance, and will use the actual constructor from the type.
Since NSubstitute does not have the equivalent of Mock
or Fake
from other popular mocking frameworks, AutoFixture had to provide a special specimen builder, called SubstituteRelay
, to fill in the gap. You can use this class as any other specimen builder to instruct the Fixture to return a mock when a specific type instance is requested.
QUESTION
I have an old project (VS2105) I'm unable to use the dotnet
command so I'm trying to use coverlet.msbuild
task.
MSBuild.exe my-solution.sln /t:My_Project_Test:InstrumentModules /t:My_Project_Test:GenerateCoverageResult /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Include="[*]*"
But it returns zero data
...ANSWER
Answered 2021-Oct-21 at 17:58You need to include the IncludeTestAssembly
property to work:
QUESTION
I am attempting to test an async Task
method that calls synchronous code before and after calling a separate async Task
method. The synchronous code updates a loading state enumeration that tells the view layer what to display (e.g. a loading spinner when loading, an error message when an exception is thrown, etc.). Note that I have omitted the property changed event in this example for brevity.
ANSWER
Answered 2021-Oct-02 at 16:34You can mock LoadData
to return a task managed by you, which will complete when you say so. Then, do not await Refresh
because for this test you don't need for Refresh
to complete, you want to check the state in progress. Then after you call Refresh
(without await
) the LoadData
will be in progress and you can verify that state is Busy
. Then complete the LoadData
and wait for Refresh
to complete to finish the test. For example:
QUESTION
I've got a .NET Core Xunit project targeting .NET 4.6 which is using NSubstitute 3.1.0. I'm setting up a mocked object which needs to return different things based on different arguments received. Here's some sample code (and a link to the github repo for the source code of the full project):
...ANSWER
Answered 2021-Aug-20 at 02:18Thanks for the repo. I believe this is occurring as the process of stubbing the second call ends up invoking the first stub with a null
argument (Arg.Is
returns null
). NSubstitute realises that this call doesn't match, and so handles the NullReferenceException
. The test should still work as required -- it just affects debugging when "break on exception" is enabled.
If you update to NSubstitute 4.x this case should be handled without the exception as there is some code to detect that a call is being stubbed and so the first stub will not be checked.
QUESTION
I am trying to write some unit tests. The application has a number of external API calls that I would like to mock using NSubstitute. The issue is these calls use service objects that need to be instantiated in the function and can't be passed in the constructor after substitution.
For example, in the code below I am adding an account to Quickbooks:
...ANSWER
Answered 2021-Jul-23 at 15:40You cannot mock a local variable. You can create a virtual method which will return QuickbooksBO
and substitute it.
QUESTION
I'm using AutoFixture in my unit tests. The SUT is using AutoMapper. During execution of the unit test, I noticed that IMapper (interface of AutoMapper) was getting mocked by AutoFixture and thus disregarding any mapping profiles I have in my code.
So I wrote a customization to be able to pass in an IMapper instance so that AutoFixture will not mock this interface. But now it looks like that AutoFixture won't mock any other interfaces as well. This used to work before I introduced the AutoMapper customization.
...ANSWER
Answered 2021-Jul-12 at 07:39AutoFixture can only build upon the testing framework only as much as it allows itself to be extended.
In xUnit 2 each provided DataAttribute
is considered, a distinct provider of arguments for the test, which is why they are isolated during runtime.
The recommended option here is to create a data attribute that will apply both AutoMoq and the AutoMapper customization.
You can achieve this by using a CompositeCustomization
and then using it in a custom AutoDataAttribute
implementation.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install NSubstitute
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page