CakePHP Unit Testing Fixture Name Conventions Argh?

做~自己de王妃 提交于 2019-12-10 21:39:00

问题


I've been bashing my head against the wall trying to figure out why I can't get my fixture to load properly. When I attempt to run my test, my layout is rendered. If I comment out the fixture, the test runs properly. I've gone over this 100 times and I can't seem to see what's wrong.

Heres my Videosview.test.php

App::import('Model','Videosview');

class VideosviewTest extends Videosview {
    var $name = 'Videosview';
    //var $useDbConfig = 'test_suite';
}

class VideosviewTestCase extends CakeTestCase {
    var $fixtures = array( 'app.videosview' );

    function testIncrementTimer() {
        $this->autoLayout = $this->autoRender = false;

        $this->Videosview =& ClassRegistry::init('Videosview');
        //$video_view = $this->find('first');
        $result = $this->Videosview->increment_timer($video_view['Videosview']['video_id'],$video_view['Videosview']['user_id'],1);
        $this->assertTrue(true);
    }
}

This is my videosview_fixture.php

class VideosviewFixture extends CakeTestFixture {
    var $name = 'Videosview';

    var $import = array('model' => 'Videosview', 'records' => true);
}

回答1:


Lot of strange code there, it looks like you don't understand how test are used. The problem doesn't come from your fixture import.

$this->assertTrue(true) will always return true. There is no need to declare VideosviewTest.

As I don't know what your increment_timer method is supposed to do I cannot write a test for it, but let's suppose it returns the value passed + 1:

function increment_timer($id = null){
    return $id++;
}

Your test case should be

App::import('Model', 'Videosview');

class VideosviewTestCase extends CakeTestCase {
    var $fixtures = array('app.videosview');

    function startTest() {
        $this->Videosview =& ClassRegistry::init('Videosview');
    }

    function endTest() {
        unset($this->Videosview);
        ClassRegistry::flush();
    }

    function testIncrementTimer() {
        $input = 1;

        // let's test increment_timer function by asserting true that return value is $input + 1, green bar
        $this->assertTrue( $this->Videosview->increment_timer($input) == ($input+1), 'Should return 2' );
        // let's test increment_timer function by asserting false that return value is $input + 2, green bar
        $this->assertFalse( $this->Videosview->increment_timer($input) == ($input+2), 'Should not return 3' );
        //the following returns an error as return value is not equal to $input + 2, Red bar
        $this->assertTrue( $this->Videosview->increment_timer($input) == ($input+2), 'Should return 2' );
    }
}

This is what you should get, and have expected



来源:https://stackoverflow.com/questions/7408936/cakephp-unit-testing-fixture-name-conventions-argh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!