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.
Do you have Mockery installed?
If not, update your composer.json:
"require-dev": {
"mockery/mockery": "dev-master#dev"
}
Then run:
composer update
Related
When i execute the plugins/SamplePlugin test cases, it executing perfect except the controller functions which are related to urls.
The test case function like
public function testIndex()
{
$this->get('/sample-plugin /mycontroller/index');
$this->assertResponseOk();
}
when i execute the above testcase the exception
There was 1 error:
1) SamplePlugin\Test\TestCase\Controller\MyControllerTest::test
Index
include(D:\xampp\htdocs\EATZ_V2_3.X\vendor\cakephp\cakephp\tests\test_app\config\routes.php): failed to open stream: No such file or directory
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:974
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:974
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\Routing\Router.php:547
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:451
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:392
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:312
D:\xampp\htdocs\MyApp\vendor\cakephp\cakephp\src\TestSuite\IntegrationTestCase.php:233
D:\xampp\htdocs\MyApp\plugins\SamplePlugin\tests\TestCase\Controller\MyControllerTest.php:29
D:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
D:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
FAILURES!
Tests: 30, Assertions: 42, Errors: 1.
please resove the issue.thanks in advance!!!
The error states that the routes.php file is missing in the folder config. Refer to the CakePHP 3 Documentation to create a meaningful routes.php.
I want to disable the DB writing when functional testing a controller in phpunit.
In a project in Symfony2, I use phpunit to test a POST call to an API endpoint that saves to database. This worked and I now have a regression problem when activating FOSUser + HWIOAuth.
To avoid the phpunit writing dummy data to the database, I use this trick here: Testing Controllers in Symfony2 with Doctrine to mock the entity manager, then inject the mocked service into the container of the testing system.
This way when you run the test, the DB endpoint is mocked and it is only tested that a flush() is called thanks to the $entityManagerMock->expects($this->once())->method('flush'); that you can see in that question and in the code below.
This used to work.
This test gave green bar, POST calls were tested and no data was saved to the database.
public function testPostCreatesIssue()
{
$client = static::createClient();
$entityManagerMock = $this->getMockBuilder( 'Doctrine\ORM\EntityManager' )
->setMethods( array( 'persist', 'flush' ) )
->disableOriginalConstructor()
->getMock();
$entityManagerMock->expects( $this->once() )
->method( 'flush' );
$client->getContainer()->set( 'doctrine.orm.default_entity_manager', $entityManagerMock );
$postData = array
(
'title' => 'Issues controller test, post creates issue',
'body' => 'The test tests that a post call' . PHP_EOL . 'creates a new issue' . PHP_EOL . 'in the database.' . PHP_EOL . 'UTF8: áéíóú àèìòù ñç',
'pageUrl' => 'file://Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php'
);
$crawler = $client->request( 'POST', '/issues/', $postData );
$response = $client->getResponse();
$this->assertEquals( Response::HTTP_CREATED, $response->getStatusCode() );
$this->assertEquals( 'application/json', $response->headers->get( 'content-type' ) );
}
At that moment, I only had the default bundles + my own bundles in the AppKernel.php
The problem
When I activate those bundles in the AppKernel.php
new FOS\UserBundle\FOSUserBundle(),
new HWI\Bundle\OAuthBundle\HWIOAuthBundle(),
and after configuration, everything works properly testing manually in my browser, including the ajax call that does the POST tested above. That controller has nothing to do with the FOSUser and HWIOAuth as that controller worked before I activated those bundles in the kernel.
But despite the manual test did work, the automated test started to fail giving Call to a member function getRepository() on a non-object not in a line of my code but in the very internals of the FOS and the Doctrine - here's the full output:
xavi#bromo:/files/custom_www/antique-crayon/preproduction$ phpunit -c app --filter Post src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /files/custom_www/antique-crayon/preproduction/app/phpunit.xml.dist
PHP Fatal error: Call to a member function getRepository() on a non-object in /files/custom_www/antique-crayon/preproduction/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php on line 759
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 7. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
PHP 8. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
PHP 9. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
PHP 10. PHPUnit_Framework_TestCase->runTest() /usr/share/php/PHPUnit/Framework/TestCase.php:804
PHP 11. ReflectionMethod->invokeArgs() /usr/share/php/PHPUnit/Framework/TestCase.php:942
PHP 12. Xmontero\AntiqueCrayon\MainBundle\Tests\Controller\IssuesControllerTest->testPostCreatesIssue() /files/custom_www/antique-crayon/preproduction/src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php:0
PHP 13. Symfony\Component\BrowserKit\Client->request() /files/custom_www/antique-crayon/preproduction/src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php:41
PHP 14. Symfony\Bundle\FrameworkBundle\Client->doRequest() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/BrowserKit/Client.php:327
PHP 15. Symfony\Component\HttpKernel\Client->doRequest() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Client.php:111
PHP 16. Symfony\Component\HttpKernel\Kernel->handle() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Client.php:81
PHP 17. Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2330
PHP 18. Symfony\Component\HttpKernel\HttpKernel->handle() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:3080
PHP 19. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2931
PHP 20. Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2958
PHP 21. Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->preProcess() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:107
PHP 22. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->getListeners() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php:215
PHP 23. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->lazyLoad() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:128
PHP 24. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php:188
PHP 25. appTestDebugProjectContainer->getProfilerListenerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 26. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2251
PHP 27. appTestDebugProjectContainer->getProfilerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 28. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2234
PHP 29. appTestDebugProjectContainer->getSecurity_ContextService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 30. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:2374
PHP 31. appTestDebugProjectContainer->getSecurity_Authentication_ManagerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 32. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:3941
PHP 33. appTestDebugProjectContainer->getFosUser_UserProvider_UsernameEmailService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 34. Symfony\Component\DependencyInjection\Container->get() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:3885
PHP 35. appTestDebugProjectContainer->getFosUser_UserManagerService() /files/custom_www/antique-crayon/preproduction/app/bootstrap.php.cache:2056
PHP 36. FOS\UserBundle\Doctrine\UserManager->__construct() /files/custom_www/antique-crayon/preproduction/app/cache/test/appTestDebugProjectContainer.php:1657
PHP 37. Doctrine\ORM\EntityManager->getRepository() /files/custom_www/antique-crayon/preproduction/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Doctrine/UserManager.php:40
xavi#bromo:/files/custom_www/antique-crayon/preproduction$
Narrowing down the problem
The problem is the service doctrine.orm.default_entity_manager for which I inject a mock during the test phase, this is why the manual test in the browser does not fail: No mocks, there.
If I edit my test to do not use any mock, just write to the real DB, it then works:
To test without mock I just comment out the ->set() in the container. And to avoid an Method was expected to be called 1 times, actually called 0 times. I also comment out the ->expects():
//$entityManagerMock->expects( $this->once() )
// ->method( 'flush' );
//
//$client->getContainer()->set( 'doctrine.orm.default_entity_manager', $entityManagerMock );
then the tests greenbars:
xavi#bromo:/files/custom_www/antique-crayon/preproduction$ phpunit -c app --filter Post src/Xmontero/AntiqueCrayon/MainBundle/Tests/Controller/IssuesControllerTest.php
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /files/custom_www/antique-crayon/preproduction/app/phpunit.xml.dist
.
Time: 0 seconds, Memory: 32.50Mb
OK (1 test, 2 assertions)
xavi#bromo:/files/custom_www/antique-crayon/preproduction$
Conclusions
Injecting a mock in doctrine.orm.default_entity_manager pre-activating FOSUser and HWIOAuth, works fine
When activating these bundles in the AppKernel the trick does not work.
I then only can test allowing to write real data to a database, which is not desired when testing.
Question
How can I disable the writing to the DataBase when calling a controller from a test in phpunit, when FOSUserBundle and HWIOAuthBundle are activated in the kernel?
Thanks!
Xavi.
I'm not proud about it but I haven't done so many test with phpUnit to answer your question with a 100% of security on the answer, but here is my opinion.
I think that the solution is to mock the entityManager, as you are doing yet, but you are moking only the persist() and flush() method.
->setMethods( array( 'persist', 'flush' ) )
->disableOriginalConstructor()
Without the code you are trying to test I can't know it for sure, but it seems that in any part of your controller you're using de FOSUserBundle or the HWIOAuthBundle and one or both of them is using at least the getRepository() method and you need to mock at least all the methods that can be used via FOSUserBundle and HWIOAuthBundle.
If you don't indicate the methods with the setMethods() all of them are mocked, and you can still have controll with the number of times a method is called or not as in the code example below.
$mockObjectManager = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$mockObjectManager->expects($this->any())
->method('persist');
$mockObjectManager->expects($this->atLeastOnce())
->method('flush');
If the FOSUserBundle is trying to get the repository, as it seems, you will have to mock the return of that call, if you don't do that it will probably end with an error at the next call, an example:
$this->em->expects($this->any())
->method('getRepository')
->with('FOSUserBundle:XXXXX')
->will($this->returnValue($this->getMockedClassWithFind('\Doctrine\ORM\EntityRepository')));
I think the best solution is to start resolving this first error, you will see another error because the object the FosUserBundle is expecting will be null, and mock that one, and maybe expects another object or maybe you're done.
Hope is helpful.
I try to start using phpunit on my laravel project.
I call "phpunit" and got this error:
$ phpunit
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /home/bee/www/postaler/phpunit.xml
EPHP Fatal error: Cannot redeclare Helper\isMenuActive() (previously declared in /home/bee/www/postaler/app/helpers.php:4) in /home/bee/www/postaler/app/helpers.php on line 6
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:130
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:192
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:325
PHP 6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:772
PHP 9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:751
PHP 10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:649
PHP 11. Illuminate\Foundation\Testing\TestCase->setUp() /usr/share/php/PHPUnit/Framework/TestCase.php:801
PHP 12. Illuminate\Foundation\Testing\TestCase->refreshApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:31
PHP 13. Illuminate\Foundation\Application->boot() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:48
PHP 14. Illuminate\Foundation\Application->bootApplication() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:552
PHP 15. Illuminate\Foundation\Application->fireAppCallbacks() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:569
PHP 16. call_user_func:{/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP 17. TestCase->{closure:/home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:223-271}() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:792
PHP 18. require() /home/bee/www/postaler/vendor/laravel/framework/src/Illuminate/Foundation/start.php:239
my helper code
<?php namespace Helper;
//app/helpers.php
function isMenuActive($url_segment) {
if (\Request::segment(1) == $url_segment) { return " active"; }
}
I included the helper file in app/start/global.php
require app_path().'/helpers.php'
my test script
<?php
// app/tests/FromTest.php
class FromTest extends TestCase {
public function testFrom()
{
$response = $this->call('GET', 'user/profile');
$this->assertTrue(true);
}
}
app/start/global.php is executed once for each request and should not be used for class/function/constant definitions. Instead, add a files autoload rule to your composer.json.
"files": ["src/functions.php"],
I am trying to configure Magento test automation framework on my system.
When I run phpunit in command line, I am getting following error. Same error I am getting while running test in the netbeans.
Strict Standards: Declaration of Mage_Selenium_Driver::doCommand() should
be compatible with that of PHPUnit_Extensions_SeleniumTestCase_Driver::doCommand()
in C:\MTAF\taf\lib\Mage\Selenium\Driver.php on line 38
..
Fatal error: Call to undefined method PHPUnit_Framework_TestSuite::isPublicTestMethod() in C:\MTAF\taf\lib\Mage\Selenium\TestCase.php on line 2502
Can some one please suggest some solution for the same.
I changed the code, this is the change I performed:
--- a/framework/Mage/Selenium/TestCase.php
+++ b/framework/Mage/Selenium/TestCase.php
## -409,7 +409,7 ## class Mage_Selenium_TestCase extends PHPUnit_Extensions_SeleniumTestCase
$testMethods = array();
$class = new ReflectionClass(self::$_testClass);
foreach ($class->getMethods() as $method) {
- if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
+ if ($method->isPublic()) {
$testMethods[] = $method->getName();
}
}
I was getting this error when running phpunit >= 4.0. Downgrading to 3.7.x solved it for me.
I'm learning grails and read Grails In Action book. Try perform some tests from it, but got strange behaviour for me. I have next simple integration test:
#Test
public void testProjections() throws Exception {
User user1 = new User(mail: 'test1#test.tld', password: 'password1').save(flush: true)
User user2 = new User(mail: 'test2#test.tld', password: 'password2').save(flush: true)
assertNotNull(user1)
assertNotNull(user2)
// Chain add Tag to Post
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
// Separate add tag to post
Post post = user1.posts.iterator().next()
Tag tag1 = new Tag(name: 'tag-1')
post.addToTags(tag1)
// http://stackoverflow.com/questions/6288991/do-i-ever-need-to-explicitly-flush-gorm-save-calls-in-grails
// Have tried with and without next line without success:
//sessionFactory.getCurrentSession().flush()
assertEquals(['tag-0', 'tag-1'], user1.posts.iterator().next().tags*.name.sort()) // line 154
…
}
Then I run it twice subsequently:
grails>
grails> test-app -rerun -integration
| Running 5 integration tests... 2 of 5
| Failure: testProjections(com.tariffus.QueryIntegrationTests)
| java.lang.AssertionError: expected:<[tag-0, tag-1]> but was:<[tag-1]>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:154)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails>
grails> test-app -rerun -integration
| Running 5 integration tests... 2 of 5
| Failure: testProjections(com.tariffus.QueryIntegrationTests)
| java.lang.AssertionError: expected:<[3, 1, 2]> but was:<[[tag-1, tag-2, tag-0, tag-5, tag-3, tag-4], [tag-6]]>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:743)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.tariffus.QueryIntegrationTests.testProjections(QueryIntegrationTests.groovy:164)
| Completed 5 integration tests, 1 failed in 0m 0s
| Tests FAILED - view reports in /home/pasha/Projects/grails/com.tariffus/target/test-reports
grails>
As you can see first fails on line 157 and second, runned just after that in second without any modification goes further.
I use Postgres database and environment test configured dataSource in mode dbCreate = 'update'.
What I do incorrect and why it works sometimes?
I would say that a source of problem is this line:
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
These dynamic addTo* methods does not propagate save to the associated instances until save() is called on the parent instance. So calling save() on user1 after should fix it:
user1.addToPosts(new Post(content: 'First').addToTags(new Tag(name: 'tag-0')))
user1.save()
This should propagate save() to Post instance at first and then to Tag instance transitively.