© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•12mo ago•
6 replies
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Integration testing
C#CC# / help
16mo ago
Integration Testing
C#CC# / help
4y ago
Unit/Integration testing - what are good approaches?
C#CC# / help
2y ago
✅ Testing Guidance (Unit/Integration)
C#CC# / help
4mo ago