I use laravel (4.1) framework and i read "Laravel-testing-decoded", it's a ebook by Jeffrey Wey.
I want to test my modal User and my method setPasswordAttribute($password)
My unit-testing :
<?php
class UserTest extends TestCase {
public function testHashesPasswordWhenSet(){
Hash::shouldReceive('make')->once()->andReturn('hashed');
$user = new User;
$user->password = 'food';
$this->assertEquals('hashed', $user->password);
}
}
But when i launch CLI : phpunit
it return me a error : Fatal error: Class 'Mockery' not found
In complete error :
Fatal error: Class 'Mockery' not found in /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 84
Call Stack:
0.0021 236384 1. {main}() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/composer/bin/phpunit:0
0.0294 1425104 2. PHPUnit_TextUI_Command::main() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/composer/bin/phpunit:63
0.0294 1425336 3. PHPUnit_TextUI_Command->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:129
0.0692 3626416 4. PHPUnit_TextUI_TestRunner->doRun() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/Command.php:176
0.0741 3944720 5. PHPUnit_Framework_TestSuite->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/TextUI/TestRunner.php:349
0.0741 3946368 6. PHPUnit_Framework_TestSuite->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:705
0.0742 3946968 7. PHPUnit_Framework_TestSuite->runTest() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:745
0.0742 3947000 8. PHPUnit_Framework_TestCase->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:775
0.0743 3948232 9. PHPUnit_Framework_TestResult->run() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:783
0.0754 4005504 10. PHPUnit_Framework_TestCase->runBare() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestResult.php:648
0.2926 15417592 11. PHPUnit_Framework_TestCase->runTest() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:838
0.2926 15418872 12. ReflectionMethod->invokeArgs() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
0.2926 15418904 13. UserTest->testHashesPasswordWhenSet() /Applications/MAMP/htdocs/ptf/vendor/phpunit/phpunit/PHPUnit/Framework/TestCase.php:983
0.2928 15426728 14. Illuminate\Support\Facades\Facade::shouldReceive() /Applications/MAMP/htdocs/ptf/app/tests/models/UserTest.php:7
0.2928 15426944 15. Illuminate\Support\Facades\Facade::createFreshMockInstance() /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:50
0.2928 15427040 16. Illuminate\Support\Facades\Facade::createMockByName() /Applications/MAMP/htdocs/ptf/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:64
I don't understand, why i have this error.
Because if you start googling around for "Mockito cannot mock this class CrudRepository" you'll hit a lot of articles about how Spring Boot (particularly in regards to the @WebMvcTest annotation) creates the application context and when beans are available and what name they have when they're made available and all that.
Some of the existing tests were using Mockery’s instance mocks to workaround older code that was new ing up classes instead of relying on the service container. My newer tests and code were using the container and normal mocks.
Spies are similar to mocks; however, spies record any interaction between the spy and the code being tested, allowing you to make assertions after the code is executed: // ... Unlike traditional static method calls, facades (including real-time facades) may be mocked.
When testing Laravel applications, you may wish to "mock" certain aspects of your application so they are not actually executed during a given test. For example, when testing a controller that dispatches an event, you may wish to mock the event listeners so they are not actually executed during the test.
Do you have Mockery installed? If not, update your composer.json:
"require-dev": {
"mockery/mockery": "dev-master@dev"
}
Then run:
composer update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With