Answer the question
In order to leave comments, you need to log in
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()});
// какая то логика тестирования
}
Answer the question
In order to leave comments, you need to log in
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question