C
C#3mo ago
bribri

integration testing my moqs are not working

I have made this code. The current account service gets the user account id with logged in/api key .... But for some reason when i moq it just gives null
public class AddArticleToFavoritesTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly Mock<ICurrentAccountService> _mock;

public AddArticleToFavoritesTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
_mock = new Mock<ICurrentAccountService>();
}

[Fact]
public async Task AddArticleToFavoritesShouldReturnSucces()
{

_mock.Setup(x => x.GetCurrentAccountIdAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);

var client = _factory.CreateClient();

var requestBody = new { articleId = 1138 };
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

var response = await client.PostAsync("/api/Articles/AddToFavorites", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
public class AddArticleToFavoritesTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly Mock<ICurrentAccountService> _mock;

public AddArticleToFavoritesTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
_mock = new Mock<ICurrentAccountService>();
}

[Fact]
public async Task AddArticleToFavoritesShouldReturnSucces()
{

_mock.Setup(x => x.GetCurrentAccountIdAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);

var client = _factory.CreateClient();

var requestBody = new { articleId = 1138 };
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

var response = await client.PostAsync("/api/Articles/AddToFavorites", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
In the query handler
var accountId = await currentAccountService.GetCurrentAccountIdAsync(cancellationToken);
var accountId = await currentAccountService.GetCurrentAccountIdAsync(cancellationToken);
the iCurrentAccountservice
Task<int?> GetCurrentAccountIdAsync(CancellationToken cancellationToken);
Task<int?> GetCurrentAccountIdAsync(CancellationToken cancellationToken);
the currentaccount service is a Singleton with Di and i'm using clean architecture. I tried alot but I cant get GetCurrentAccountIdAsync to return an id
6 Replies
Sehra
Sehra3mo ago
don't see you registering the mock
var client = _factory
.WithWebHostBuilder(b =>
{
b.ConfigureTestServices(s =>
{
s.AddSingleton(_mock.Object);
});
})
.CreateClient();
var client = _factory
.WithWebHostBuilder(b =>
{
b.ConfigureTestServices(s =>
{
s.AddSingleton(_mock.Object);
});
})
.CreateClient();
bribri
bribriOP3mo ago
Ohhhh i need to register it? Inside the tests ?
Sehra
Sehra3mo ago
hard for DI to find your mock unless you tell it about it
bribri
bribriOP3mo ago
Yeah stupid me thanks
Sehra
Sehra3mo ago
you can see more info about using WebApplicationFactory at https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-9.0
Integration tests in ASP.NET Core
Learn how integration tests ensure that an app's components function correctly at the infrastructure level, including the database, file system, and network.
bribri
bribriOP3mo ago
Thanks you so much!!

Did you find this page helpful?