C#C
C#10mo 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);
        }


In the query handler
            var accountId = await currentAccountService.GetCurrentAccountIdAsync(cancellationToken);


the iCurrentAccountservice
  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
Was this page helpful?