PHPUnit:测试数据库出现问题
我是使用 PhpUnit 进行数据库测试的新手。我从简单的测试开始,将 PostgreSQL 表的内容与 xml 文件进行比较。
问题是断言无法按预期工作:
请查看结果:
http://www3.picturepush.com/photo/a/5540556/1024/Anonymous/Screenshot.png
如您所见,这些表是相等的,但是 db 表中的内容(首先在屏幕上) )有一个额外的空格...
我不知道出了什么问题。
这是 PHP 代码:
public function testGetSourceData()
{
include_once(
sfConfig::get('sf_lib_dir')
. '/task/ShopCategoryTreeUpdateTask.class.php'
);
$method = new ReflectionMethod(
'ShopCategoryTreeUpdateTask', '_getSourceData'
);
$method->setAccessible(TRUE);
$res = $method->invoke(new ShopCategoryTreeUpdateTask(new sfEventDispatcher, new sfFormatter),123);
$actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
$actual->addTable('shop');
$expected = $this->getDataSet();
$this->assertDataSetsEqual(
$actual,
$expected
);
}
这里是 XML: http://pastebin.com/5MmtJDr6
感谢您的任何目标!
I'm new to database testing with PhpUnit. I'm starting with simple test that compare PostgreSQL table's content with xml file.
The problem is that assertion doesn't work as expected:
Please take a look of the result:
http://www3.picturepush.com/photo/a/5540556/1024/Anonymous/Screenshot.png
As you see the tables are equal, but the content in db table(first at screen) has an extra spaces...
I've no idea what is wrong.
Here is PHP code:
public function testGetSourceData()
{
include_once(
sfConfig::get('sf_lib_dir')
. '/task/ShopCategoryTreeUpdateTask.class.php'
);
$method = new ReflectionMethod(
'ShopCategoryTreeUpdateTask', '_getSourceData'
);
$method->setAccessible(TRUE);
$res = $method->invoke(new ShopCategoryTreeUpdateTask(new sfEventDispatcher, new sfFormatter),123);
$actual = new PHPUnit_Extensions_Database_DataSet_QueryDataSet($this->getConnection());
$actual->addTable('shop');
$expected = $this->getDataSet();
$this->assertDataSetsEqual(
$actual,
$expected
);
}
and here the XML:
http://pastebin.com/5MmtJDr6
Thanks for any goals!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可能使测试过于复杂化。在您的方法中,QueryDataSet 和 getDataSet 可能以不同的方式将结果呈现为字符串。
您实际上不应该通过反射来测试私有和公共方法,因为这些方法应该可以通过您的公共方法进行测试和访问。如果它们不能通过公共方法访问,那么它们永远不会被调用。您的代码覆盖率报告在这里会派上用场。
I think you may be over complicating the test. In your method it is likely that QueryDataSet and getDataSet are rendering the result to string differently.
You shouldn't really be testing private and public methods by reflection because these should be testable and accessible by your public methods. If they are not accessibly by public methods then they can never be called. Your code coverage report comes in handy here.