K
K
Kenan2016-12-08 13:50:04
Laravel
Kenan, 2016-12-08 13:50:04

Should I Mock Eloquent in Laravel for Unit Tests?

As far as I know, unit tests should not depend on external entities, including working with the database, and these entities are usually replaced with mocks / stubs. But most of the methods and functions in the web application somehow work with the database.
Does it make sense to substitute work with the database (write a fake Eloquent, etc.) in unit tests? Do you need unit tests at all, or is it enough to write only functional ones?
Who acts in such situations, who writes tests?
PS Maybe of course I don't understand the meaning of unit-tests. If not, please correct.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vyacheslav Plisko, 2016-12-08
@nikchar

Tests should be modular, everything needs to be mocked up and uncoated. Separately, it is worth starting a folder with integration tests, it is not necessary to mock there, but you also need to remove the coverage separately from unit tests. The main thing is not to mix these tests.

N
Nimfus, 2016-12-08
@Nimfus

The unit test should not depend on the database, this test should check the logic. If the test is tied to getting an entity from the database, and so on, then you need to use repositories with the appropriate interfaces, and what they return can be imitated.

A
Alexander Aksentiev, 2016-12-08
@Sanasol

When running tests in laravel, the base from the migrations is rolled up: /tests/TestCase.php
For tests, a separate .env.test file with a test base.
Then work with her if necessary. It is filled if necessary, functions are run.
To check how the written fake base works? And on the real, everything will fall in the end.
True, the base is punished at the beginning once.
To exclude the influence of tests on each other, I started each test with cleaning and migration over a new one.
Well, that is, if the need arises. I don't think this is normally necessary.
I'm not a great test writer, maybe it's wrong at all, but it works :)
Automatic testing through travis-ci also works fine with this option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question