简体   繁体   English

如何在CakePHP中打开浏览器以使用DBUnit进行测试?

[英]How to open browser for testing with DBUnit in CakePHP?

I'm using CakePHP and DBUnit for database testing. 我正在使用CakePHP和DBUnit进行数据库测试。 What I'd like to achieve is to test whether the form I've submitted on the website, inserts the data correctly into the DB. 我想要实现的是测试我在网站上提交的表单是否将数据正确插入到数据库中。 I have now a lot of UI tests, where I test the page itself, but I've only this small amount of DB test: 我现在有很多UI测试,我在其中测试页面本身,但是我只有少量的DB测试:

<?php

class testSchema extends PHPUnit_Extensions_Database_TestCase {

    /*
    ** @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
    */
    public function getConnection() {
        $pdo = new PDO('mysql:dbname=job_manager;host=localhost','root','toor');
        $pdo->exec("set foreign_key_checks=0");
        return $this->createDefaultDBConnection($pdo, 'job_manager');
    }

    /*
    ** @return PHPUnit_Extensions_Database_DataSet_IDataSet
    */
    public function getDataSet() {
        $cascadeTruncates = true;
        return $this->createMySQLXMLDataSet(dirname(__FILE__).'/default.xml');
    }

    public function testRowCounts() {
        $this->assertEquals(3, $this->getConnection()->getRowCount('jobs'));
        $this->assertEquals(1, $this->getConnection()->getRowCount('machines'));
        $this->assertEquals(4, $this->getConnection()->getRowCount('users'));
    }

}
?>

I've tried to do something like this: 我试图做这样的事情:

$this->setBrowser('*firefox');
$this->setBrowserUrl('url');
$this->open('link');

Like I did in the UI tests, but it doesn't work (because this class is inherited from PHPUnit_Extensions_Database_TestCase and not from PHPUnit_Extensions_SeleniumTestCase) 就像我在UI测试中所做的一样,但是它不起作用(因为该类是从PHPUnit_Extensions_Database_TestCase继承的,而不是从PHPUnit_Extensions_SeleniumTestCase继承的)

Any ideas? 有任何想法吗? Thanks 谢谢

My solution at the end (maybe it can help someone later): 最后我的解决方案(也许以后可以帮助别人):

At the end I've created a bash script, and execute a UI test and a DB test after that. 最后,我创建了一个bash脚本,然后执行UI测试和DB测试。 Also I had to modify the DB class such that: 我还必须修改数据库类,以便:

public function getDataSet() {
    $cascadeTruncates = true;
    return $this->getConnection()->createDataSet();
}

So now it gets the dataset from the current DB. 因此,现在它从当前数据库获取数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM