5,852 questions
-1
votes
2
answers
73
views
How to mock ILogger and invoke via callback Console.Writeline to have invocations in test explorer?
I need to have every invocation from the ILogger interface of a test,
but I mock the ILogger interface in the class that is going to be tested, so it won't write to console and therefore the ILogger ...
3
votes
2
answers
155
views
Verify object value returns false when the value is changed in code using MOQ
I am creating a test to verify an object is passed to a method then verifying that the params match to the method in the call I am testing. The problem I am having that the object I am verifying ...
1
vote
1
answer
54
views
Poxing a mocked generic method with Moq
I provide a generic provider for Json serialization:
public interface IJsonSerializerProvider
{
T? Deserialize<T>(string value);
string Serialize(object? obj);
object? ...
1
vote
1
answer
95
views
Moq: how to create a HttpRequest object containing a HttpPostedFile?
I am working with ASP.NET/.NET 4.8 application.
My action method is as follows:
public class TestController: System.Web.Http.ApiController
{
// pseudo code
public async Task<...
4
votes
2
answers
195
views
How do I mock DbSet.Add() for Entity Framework Core?
I'm mocking a DbContext to auto-test classes that update the database.
I'm using Entity Framework Core and .NET 5.
Mock<MyDbContext> fakeDbContext = new Mock<MyDbContext>();
List<Book&...
1
vote
1
answer
268
views
How can you mock a caching service that in turn uses HybridCache?
I have a factory class that let's say for arguments sake returns a car:
public class CarFactory : ICarFactory
{
private const string carKey = "MyCarKey";
private readonly ...
0
votes
0
answers
359
views
How do you mock FusionCache with Moq?
I created a wrapper for FusionCache and am attempting to create a unit test.
This is the method:
public class FusionCacheService<TEntity> : IFusionCacheService<TEntity> where TEntity : ...
1
vote
4
answers
180
views
Unit test fails when running all tests, but not when run as a single test
One unit test keeps failing when I run all unit tests, but doesn't fail when running it as a single test. I've marked it with a comment below. When firing the RulesEngine via Session.Fire() there is ...
1
vote
1
answer
105
views
Mocking a C# method with an out parameter to return different values with Moq [duplicate]
I have a C# method with an out parameter:
public bool TryGetNext(out Bestellung? bestellung);
When setting up a mock for this method using Moq, how do I:
Correctly handle the out parameter in the ...
-1
votes
1
answer
75
views
How to call back a method instead of Action<T>? [closed]
_mockScreener
.Setup(
x => x.CaptureTargetScreen(
It.IsAny<string>(),
It.IsAny<int>(),
It.IsAny<int>(),
It.IsAny<Action<Bitmap>>()
...
0
votes
1
answer
32
views
Returning different values with multiple calls to a method in MOQ
I'm working on a unit test for a controller that calls a handler to retrieve a list of consumers multiple times.
The handler calls GetItemsAsync() and returns either a null value or a list of ...
2
votes
3
answers
209
views
Unit test with real database in repository pattern?
I have an ASP.NET Core Web API and services. To abstract EF Core's DbContext from service layer, I decided to use the repository pattern.
However, the repository contains logic that cannot be unit ...
1
vote
1
answer
190
views
Why is my global error handling not triggered when I throw a mock exception in my c# unit test?
I'm learning on how to write unit tests. From what I gathered, if I want to unit test a controller in a C# API, I need to mock/fake the inputs, the outputs and all dependencies of it. Next, I want to ...
0
votes
1
answer
225
views
How to Mock QueryAsync<dynamic> using Moq.Dapper
We have a class below that finds records in the list of tables provided in the parameters, and then deletes them within a transaction.
public async Task<IEnumerable<DataTableRecordsResponse>&...
0
votes
0
answers
54
views
C# Moq of list: retrieving added objects
I have created the following extension method for an IQueryable to use it in Entity Framework DbContext moqs in unit tests
public static DbSet<T> BuildMockDbSet<T>(this IQueryable<T>...
2
votes
3
answers
111
views
Verify BackgroundJob.Delete in Hangfire in mstest unit testing
I have a CancelScheduledJob method, taking in the Id of a backgroundjob and using the JobStorage to retrieve this Id and cancelling the matching hangfire job:
var mon = JobStorage.Current....
0
votes
1
answer
79
views
Moq fully generic Setup with class proxy attempt throws `Late bound operations cannot be performed`
PROBLEM TO SOLVE
I'm trying to create a mock of interface ISomeService using Moq library which will wrap its real implementation SomeService.
Right now I'm doing this manually like this:
var instance =...
1
vote
1
answer
84
views
Moq verify property init
How do i verify an init-call with Moq? I have the following property defined in an interface:
public string Text { init; }
I want to verify that init was called with a specific parameter. Is that ...
1
vote
0
answers
72
views
How do I verify no other calls on a moq that has sub-mocks?
In the following example, I'm getting some behavior I wouldn't expect:
[Test]
public void Test()
{
var mockGetter = new Mock<IHostedServiceGetter>();
var mockService = new Mock<...
2
votes
1
answer
51
views
Execution of interface-defined programs in C# test code
I am writing test code with xUnit and mock(moq) on C#.
However, the program through the interface cannot be executed.
The test code is as follows.
// test code
public void test1(string id, EntityA ...
1
vote
1
answer
192
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....
0
votes
0
answers
27
views
Unable to grab parameter in moq setup call [duplicate]
I am writing some unit tests for a backend that uses MS Graph to communicate with Azure, specifically the endpoints to add and remove users in Entra groups. But when writing the test for removing ...
0
votes
1
answer
80
views
Mock GroupBy method with in-memory database
The following method works perfectly with SQL Server, no issues.
public async Task<IEnumerable<HashRecord>> GetLatestHashes(long jobId, CancellationToken token)
{
var context = ...
1
vote
1
answer
218
views
Using Moq with a template method
I am trying to Moq a Rabbit MQ method.
ValueTask BasicPublishAsync<TProperties>(string exchange, string routingKey,
bool mandatory, TProperties basicProperties, ReadOnlyMemory<byte> ...
2
votes
1
answer
36
views
While unit testing GetAsync, How to make an HttpResponseMessage returns an exception to be caught by the catch block?
I'm unit testing a code similar to the one below. There is one use case where I want the GetAsync to throw an exception to be caught in the catch block.
try
{
var response = await client.GetAsync(...