© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
ekhidna

Unit Testing with NUnit

Hello everyone. I got this service that is called by a controller and gets data in the Db.
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync() {

    List<ApplicationUser> users = await _userManager.Users.ToListAsync();
    return users;
}
public async Task<IEnumerable<ApplicationUser>> GetAllUsersAsync() {

    List<ApplicationUser> users = await _userManager.Users.ToListAsync();
    return users;
}

Does this unit test, test anything? or because everything is Mocked it actually tests nothing?
[TestFixture]
public class UserServiceTests {
    Mock<IUserService>? _userService;

    [SetUp]
    public void Setup() {
        _userService = new Mock<IUserService>();
    }

    [Test]
    public async Task Test_UserService() {
        Guid id = Guid.NewGuid();
        ;
        IEnumerable<ApplicationUser> app = new List<ApplicationUser>()
        {
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser1", Name = "User1", Email = "user1@mail.com"},
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "user2@mail.com"},
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "user2@mail.com"},
        };

        _userService = new Mock<IUserService>(MockBehavior.Strict);
        _userService.Setup(x => x.GetAllUsersAsync()).ReturnsAsync(app);
        var users = await _userService.Object.GetAllUsersAsync();
        Assert.That(users.Count(), Is.EqualTo(3));

    }
}
[TestFixture]
public class UserServiceTests {
    Mock<IUserService>? _userService;

    [SetUp]
    public void Setup() {
        _userService = new Mock<IUserService>();
    }

    [Test]
    public async Task Test_UserService() {
        Guid id = Guid.NewGuid();
        ;
        IEnumerable<ApplicationUser> app = new List<ApplicationUser>()
        {
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser1", Name = "User1", Email = "user1@mail.com"},
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "user2@mail.com"},
            new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "user2@mail.com"},
        };

        _userService = new Mock<IUserService>(MockBehavior.Strict);
        _userService.Setup(x => x.GetAllUsersAsync()).ReturnsAsync(app);
        var users = await _userService.Object.GetAllUsersAsync();
        Assert.That(users.Count(), Is.EqualTo(3));

    }
}

Because the method GetAllUsersAsync appears green as if it was tested. But if I Debug the test it never goes into the function in the code. I guess that's because everything is mocked. Not sure. Some help here. Thank you
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

❔ Proper mocking for Unit Testing (NUnit)
C#CC# / help
4y ago
unit testing
C#CC# / help
2y ago
Unit testing
C#CC# / help
2y ago
✅ Unit testing
C#CC# / help
3y ago