Z
Z
Zhuchkin2015-08-20 14:39:10
ASP.NET
Zhuchkin, 2015-08-20 14:39:10

What is the correct way to apply unit tests to Entity Framework?

Good day! I have a question like this: I have an asp.net mvc web application that works with a database through Entity Frammework. To work with Entity, I created a provider class that performs standard CRUD operations. I would like to cover the whole thing with unit tests like tough men. But I heard that unit tests should not work with the database. And this seems to be logical - let's say I want to test insert, update and delete operations from my repository, but it will be strange if the test code performs these operations with a real database (even if it is a test one).
An example of a test using Moq found on the Internet

// интерфейс репозитория
    interface IRepository : IDisposable
    {
        List<Computer> GetComputerList();
        Computer GetComputer(int id);
        void Create(Computer item);
        void Update(Computer item);
        void Delete(int id);        
    }

     // тестовый метод
    [TestMethod]
    public void IndexViewBagMessage()
    {        
        var mock = new Mock<IRepository>();
        mock.Setup(a => a.GetComputerList()).Returns(new List<Computer>() { new Computer()});
         // какая то логика тестирования
    }

But then the question arises - what is the point of all this? After all, in the mock we substitute fake data. That is, I kind of want to test the operation of my repository, but instead, the methods of the repository itself are not used, but dummy data from the mock is used instead.
Hence the question: how will it be correct to test the repository and what is the meaning of the example I found?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-08-20
Protko @Fesor

the repository is not covered by unit tests (otherwise it will turn out that you will not be testing your repository, but the Entity Framework) That's all. Use integration/functional tests.

O
Oxoron, 2015-08-21
@Oxoron

If you want to test the repository, mock EF.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question