Question

Here's the situation:
I'm trying to create test cases for my model that uses a datasource. The datasource connects to an API.

What I tried:
I tried to set the $useDbConfig var of the fixture to the datasource like so.

class ProjectFixture extends CakeTestFixture {
    public $useDbConfig = 'project';
    public $useTable = false;
}

Then on my model test here's what I did:

App::uses('Project', 'Model');

class ProjectTest extends CakeTestCase {
    public $fixtures = array('app.project');

    public function setUp() {
        parent::setUp();
        $this->Project = ClassRegistry::init('Project');
    }
...

However when I run the test, I get this error msg:

Invalid datasource project for object Project
Error: An Internal Error Has Occurred.

Stack Trace
CORE/Cake/TestSuite/Fixture/CakeFixtureManager.php line 144 → CakeTestFixture->__construct()
CORE/Cake/TestSuite/Fixture/CakeFixtureManager.php line 78 → CakeFixtureManager->_loadFixtures(array)
CORE/Cake/TestSuite/CakeTestRunner.php line 52 → CakeFixtureManager->fixturize(ProjectTest)
CORE/Cake/TestSuite/CakeTestSuiteCommand.php line 111 → CakeTestRunner->doRun(PHPUnit_Framework_TestSuite, array)
CORE/Cake/TestSuite/CakeTestSuiteDispatcher.php line 242 → CakeTestSuiteCommand->run(array)
CORE/Cake/TestSuite/CakeTestSuiteDispatcher.php line 99 → CakeTestSuiteDispatcher->_runTestCase()
CORE/Cake/TestSuite/CakeTestSuiteDispatcher.php line 116 → CakeTestSuiteDispatcher->dispatch()
APP/webroot/test.php line 92 → CakeTestSuiteDispatcher::run()

Has anyone tried to do this before? I've been searching through the web but can't seem to find any similar resource.

Was it helpful?

Solution

A little bit more tinkering and I was able to get this to work. It doesn't use Fixtures but it gets the job done. Here's what I did on the model test file:

App::uses('Project', 'Model');

class ProjectTest extends CakeTestCase {
    public function setUp() {
        parent::setUp();
        $this->Project = ClassRegistry::init('Project');
        $this->Project->useDbConfig = 'project';
    }
...

I've simply forced the model to use the datasource I want in the setUp.

I am now able to run my tests with this setup. Still if anyone has successfully done this through fixtures, you're help will be much appreciated.

OTHER TIPS

The only way I made it work was by using a connection called:

test_(name of the connection the actual model uses)

For example, in my case, the model ActivePermission uses the connection login, so, for the fixture I used:

public $useDbConfig = 'test_login';

Also, if you do not explicit set $useDbConfig the default for cake is to use the test connection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top