Answer the question
In order to leave comments, you need to log in
Mechanism for unit testing without database?
Yii2 uses ActiveRecord to work with the database, which is not testable. Therefore, we use a wrapper class that already uses ActiveRecord, and which can be capitulated.
Now the question arises, how to make the mock object work like an ActiveRecord, that is, have CRUD functions without using a database?
The simplest and fastest solution seems to be a mechanism using arrays.
For example, a request
$exists = User::find()->where(['username' => $username])->exists()
function exists($name, $value)
{
$user = null;
foreach($dbUsers as $dbUser) {
if ($dbUser[$name] == $value) {
return true;
}
}
return false;
}
Answer the question
In order to leave comments, you need to log in
Why don't you like the partial mock option? After all, you do not need to test CRUD as such, but the correct reaction of the surrounding code to how the ActiveRecord model behaves. Mock CRUD methods, let the rest work as is.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question