C
C#•8mo ago
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 = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "[email protected]"},
};

_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 = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser2", Name = "User2", Email = "[email protected]"},
new ApplicationUser { Id = Guid.NewGuid(), UserName = "TestUser3", Name = "User2", Email = "[email protected]"},
};

_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
1 Reply
Esa
Esa•8mo ago
This doesn't really test anything. Consider that with unit tests you want to verify your applications logic, to see that what you think happens is what actually happens. Now there isn't much logic to this piece of code. All it does is return an IEnumerable, that's it. If you want to perform an actual test of the database, that's a gnarly topic altogether, and regardless of the route chosen to perform such a task, this is not it (you'd be looking at either testing the actual database or an in-memory database created with the exact same script). So you mentioned a setup where a Controller receives a request to fetch something from a service. This service then calls deeper down the stack to the database to return something. Here's some examples that would require unit tests (these aren't really applicable to your current situation, but you can see from these what I mean by logic): Case: An unauthenticated user requests all users. Test: Assert that the response is as expected (unauthenticated, bad request etc) Case: An authenticated user requests all users. Test: Assert that the response is 200 ok with content. Case: A user requests something nonsensical. Test: Assert that your request validation does as it should. As you can see from these examples, they contain logic that is absent from your original example. With that it should become clear when we create unit tests and when we dont. 🙂
Want results from more Discord servers?
Add your server