Mock Authentication on the dao

Asked

Viewed 118 times

0

_usuario = CriaUsuario(new Login("Roberto"), new Senha("Senha"));

        var dao = new Mock<IUsuarioDao>();
        dao.Setup(d => d.Autenticar(It.IsAny<Login>(), It.IsAny<Senha>())).Returns(_usuario);


        var usuario = dao.Object.Autenticar(new Login("Roberto"), new Senha("Senha"));

What do I want to know and if it’s worth taking this kind of test? Because I look like this, I prefer to do the test accessing the database because that would test the query

anyway.. what’s the best way?

  • I don’t know if I understand your difficulty. Is this in your normal code? It seems wrong to me. You shouldn’t use your normal code to produce tests. And the tests should test exactly what the code should do. Your mock should work as if it were the database and do all checks if everything is ok with data and environment controlled. Is it easy to do this? No, but people don’t tell you about it. Is it worth a unit test? It depends on several factors.

1 answer

0


These are different types of tests.

The first, using mocks, is a unit test. Its purpose is to test the logic/rule that the method implements.

The second is an integration test. The purpose is to test the integration of this part of the system with another system, in this case the database.

If it’s worth doing the tests, I don’t know, only you can qualify their importance and quantify the cost/benefit of doing them.

If you should do the first, the second or both, you should choose the right test level for the situation.
A business class should be tested in isolation, while the DAO (maybe) needs to be tested together with the database.

  • In my case I develop with nhibernate, and interesting to test the DAO/Repositorios ? Testing then in most cases is used to test Rules only.. Since DAO is theoretically and to be simply classes of access to database and processed return of data, good and what I understood?

  • Rules are usually tested using mocks, including objects returned from the bank (unit tests). On the other hand, in this case, integration tests are used to test whether interaction with the bank is being "well done", for example if the darlings return the desired (real) objects.

  • Perfect, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.