Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

Question list filters

Filter by
Sorted by
Tagged with
Filter by Employee ID
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 ...
heringer's user avatar
  • 3,337 reputation score 3337
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 ...
Allaev Bekzod's user avatar
  • 179 reputation score 179
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 ...
Martin's user avatar
  • 2,324 reputation score 2324
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<...
Rokke's user avatar
  • 63 reputation score 63
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 ...
Yzak's user avatar
  • 125 reputation score 125
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 ...
Antonio's user avatar
  • 11.6k reputation score 11553
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 ...
JsonStatham's user avatar
  • 10.5k reputation score 10532
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() ...
hrushi's user avatar
  • 296 reputation score 296
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 ...
LockTar's user avatar
  • 5,517 reputation score 5517
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 ...
Tomcat's user avatar
  • 60 reputation score 60
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....
RoLYroLLs's user avatar
  • 3,225 reputation score 3225
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 ...
Mathias F's user avatar
  • 16.1k reputation score 16052
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(...
Carrot's user avatar
  • 3 reputation score 3
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: ...
not_only_but_also's user avatar
  • 123 reputation score 123
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 ...
GraySquid's user avatar
  • 61 reputation score 61

15 30 50 per page
1
2 3 4 5
45
Morty Proxy This is a proxified and sanitized view of the page, visit original site.