662 questions
Score of 0
0
votes
1 answer
1
answer
63 views
63
views
NSubstitute: ToDictionaryAsync: Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations
I'm using .NET 8 with NSubstitute for unit tests.
Even though I setup my repository using IQueryable and BuildMock correctly:
when calling ToListAsync, unit test is working fine
when calling ...
Advice
0
votes
4
replies
88
views
How to test if expression contains specific value with NSubstitute
In my code, I use a SingleOrDefaultAsync method, which accepts Expression<Func<T, bool> like this:
await _recipientRepository.SingleOrDefaultAsync(x => x.Id == receiverId);
The unit test ...
Advice
0
votes
1
replies
61
views
NSubstitute - Accept any argument that has been built with a specific parameter
I am trying to mock a repository that inherits from the BaseRepository class in the Ardalis.Specification.EntityFrameworkCore library. This base class exposes the ListAsync method which takes in a ...
Score of 0
0
votes
0 answers
0
answers
70 views
70
views
Setup NSubstitute return value complaints that I do not dispose the result of the mocked method
I'm using NSubstitute to mock the creation of a HttpClient when calling IHttpClientFactory.CreateClient() in an ASP.NET Core 8 Web API.
The code is as follows:
httpClientFactory = Substitute.For<...
Score of 0
0
votes
0 answers
0
answers
138 views
138
views
NSubstitute unit test fails with "Could not find a call to return from" when testing EF Core query with async extension using TransactionScope
I'm trying to write a unit test for a repository method that queries EF Core DbSet using a custom async extension method which wraps FirstOrDefaultAsync() inside a TransactionScope with ...
Score of 0
0
votes
1 answer
1
answer
106 views
106
views
How to use NSubstitute with void function and ref argument?
I'm new on C# and NSubstitute - I've been having a rough time trying to mock a avoid function and that it has a ref argument. I've been trying a combination of these docs and these docs (as well as ...
Score of 2
2
votes
1 answer
1
answer
239 views
239
views
NSubstitute Unit test mocking async method not working first time
Here is my code:
private readonly ISkuAdjustmentExclusionsRepository _skuAdjustmentExclusionsRepositoryMock = Substitute.For<ISkuAdjustmentExclusionsRepository>(); // <-- declared at the ...
Score of 0
0
votes
0 answers
0
answers
49 views
49
views
How to spy on a virtual method called from ctor
I am new to NSubstitute. I have a class (not Interface, unfortunately this is an existing class) that looks like below:
class A
{
public A()
{
aa();
}
public virtual void aa()
...
Score of 0
0
votes
1 answer
1
answer
386 views
386
views
Mocking Blobclient.DownloadToAsync(stream) using NSubstitute
I saw a different issue answer that is related but I can't get my method mocked.
I'm using the blobClient.DownloadToAsync(fileStream) method. I want to create multiple tests. A test for a download ...
Score of 2
2
votes
1 answer
1
answer
168 views
168
views
How to mock indexer return value with Any string key in NSubstitute?
I have a get-only indexer defined on an interface
public interface ISomeInterface
{
string this[string key] { get; }
}
In a Unit Test with Moq it is valid for any string parameter as the indexer ...
Score of 1
1
vote
1 answer
1
answer
242 views
242
views
Moq to NSubstitute AddScoped
I'm new to unit testing, so I'm not sure what I'm looking for. I'm trying to convert a project using Moq to NSubstitute.
Question: How do you convert this line from Moq to NSubstitute
services....
Score of 0
0
votes
2 answers
2
answers
148 views
148
views
Mock a call to a Repository that uses Ardalis Specification
I have a generic Repository (RepositoryBase) that gets queried using a specification
Specification
public sealed class ProductByIdSpec : SingleResultSpecification<Product>
{
public ...
Score of -2
-2
votes
1 answer
1
answer
77 views
77
views
Nsubstitute line I don't understand and even if removed still works fine
There is this line that ChatGPT put down when I asked it how to throw an exception with nsubstitute:
var exception = await Assert.ThrowsAsync<NullReferenceException>(() => _controller.Post(...
Score of 0
0
votes
2 answers
2
answers
183 views
183
views
Mocked dependency with an async method never returns control to caller
Using NSubstitute, I've encountered what seems to be either a bug or me not understanding how NSubstitute or async/await works.
I'm trying to test an async method which has a while loop like so:
...
Score of 2
2
votes
1 answer
1
answer
91 views
91
views
How to test the arguments of a Action that is itself passed as a method argument?
I'd first like to state that I'm new to StackOverflow, unit testing with NSubtitute and to working with actions/delegates in C# - so apologies in advance for any misuse of formatting and/or ...