lundi 24 février 2020

How to unit test DAO Update/Remove methods that return void?

I have two methods, which I'm really not sure how to test. They are in the UserService class:

    public async Task UpdateUser(int userId, UserInput userInput)
    {
        var user = await _userRepository.Get(userId);
        if (user != null)
        {
            user.Name = userInput.Name;
            _userRepository.Update(user);
        }
    }

    public async Task RemoveUser(int userId)
    {
        var user = await _userRepository.Get(userId);
        if (user != null)
        {
            _userRepository.Remove(user);
        }
    }

And here is the test I wrote for Update method:

    [Fact]
    public async void UpdateUser()
    {
        var repository = new Mock<IUserRepository>();
        repository.Setup(r => r.Get(1)).ReturnsAsync(new User(1, "John Doe"));
        repository.Setup(r => r.Update(It.IsAny<User>()));

        var userService = new UserService(repository.Object, SingletonAutoMapper.Mapper);

        var userInput = new UserInput { Name = "John Doe" };
        await userService.Update(1, userInput);
    }

So in UpdateUser, the only methods I'm using are from repository, that I'm telling exactly what to return. Moreover I don't even return anything from it (PUT web api method should return 204 No Content status).

(Mapper is used in this class, but not in these two methods, so please don't mind)

Can I leave this test without Assert method? If not, How should I test it?

Aucun commentaire:

Enregistrer un commentaire