B
B
BonBon Slick2018-12-22 10:51:20
PHP
BonBon Slick, 2018-12-22 10:51:20

How to structure dummy / mock / stub for tests?

https://phpunit.readthedocs.io/en/7.4/test-doubles.html
This is not a stub or mock, no need. a simple data object. Naturally, its creation is tested separately. However, other objects that depend on it still need it. Example
new User(new Username('Vasya'), new UserRole(new UserRoleName('admin')));
And then I test if there are VOs and their values.
What about VO?
In fact, there is a UsernameTest that checks whether the VO is working, however, these VOs are scattered across all tests, since a lot depends on them, so the question arises is it right to do this?
Perhaps it's worth making some kind of trait and creating one single mock / stub and use this trait wherever this VO is used for tests?
And what about the UserRole, because it is already an object that is associated with the user, it also has its own VOs and separate tests for its VOs, methods. However, almost the same question, how to treat such objects?
Where to place them? Whether it is normal that everywhere where the test with the user goes such creation of dummy UserRole?
Or still take out everything in general somewhere in stubs and mocks, but where? Where they are used in different unit tests

  1. The main problem is if a test fails, for example, creating a UserRole or its UserRoleName, then many tests that are related to the role and users will fail even if there is an error in the test for creating a UserRoleName.
  2. Now scattered everywhere new new new new... there is no single place to create stubs / mocks / dummy,
$test = new IndependentClass(new SomeCLass(new SomeCLass('test'), 
new SomeCLass('test'), new SomeCLass('test'), new SomeCLass('test'));


$dependsOnTestClass = new DependsClass(new SomeCLass(new SomeCLass('test'),
new SomeCLass('test'), new SomeCLass('test'), $test ));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question