D
D
DD-var2021-06-21 18:36:37
ASP.NET
DD-var, 2021-06-21 18:36:37

How to setup moq for controller?

I decided to write some unit tests for my controllers, I found moq. And I didn't fully understand how to work with it.
There is a service for adding users to the database. I would like to know what errors it will react to and what it will send. I can’t add a unit test because I don’t know what to return.

[Test]
        public void CheckAddUser()
        {
            SetingUsers();
            
            var moc = new Mock<IUserService>();
            moc.Setup(c => c.Create(It.IsAny<RegisterViewModel>()))
                .Returns<IdentityResult>("не знаю что тут возращать");
            

            var userController = new TestController(moc.Object);

            var result = userController.Create(newUser).Result;
        }


the controller itself:
public async Task<IdentityResult> Create(RegisterViewModel user)
        {
            return await _userService.Create(user);
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-06-21
@DD-var

You have not fully understood not in lib, but in yourself)
First you need to raise the question of what we are testing, if you answer it, you will know what to apply as input and what to check as output.
You have written the following "for any user model at the entrance, check that the user is being created"
Let's say that you check at the end of the IdentityResult for success. From the mock, you'll probably want to return a successful IdentityResult. But it's pointless. Because you messed up everything and are not testing any application logic, but testing a library with mocks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question