简体   繁体   English

Cakephp中具有只读自定义数据源的单元测试

[英]Unit Tests with read-only custom Datasource in Cakephp

I am currently in the process of writing some Unit Tests for my CakePHP application. 我目前正在为CakePHP应用程序编写一些单元测试。 I have a custom datasource for a few of my models. 我有一些模型的自定义datasource This is a read only datasource. 这是一个只读数据源。 I want to run some unit tests using this datasource but can't seem to get it working. 我想使用此数据源运行一些单元测试,但似乎无法正常工作。

I have created a fixture with the following at the top: 我创建了一个灯具,其顶部如下:

public $useDbConfig = 'test_datafeeds';

But I get a load of errors thrown back at me. 但是我遇到了很多错误。 If I delete all my fixtures from the DataTest it works, however it uses all the actual data in the actual database too - for my tests I would prefer to use the dummy data I have sent up for the data in my database. 如果我从DataTest中删除所有固定装置,它将起作用,但是它也会使用实际数据库中的所有实际数据-对于我的测试,我更喜欢使用我为数据库中的数据发送的虚拟数据。

I have managed to create static fixtures using dummy data to mock the datasource, but i'd also prefer the option to use the actual datasource in tests. 我已经设法使用虚拟数据来模拟数据源来创建静态装置,但是我也更喜欢在测试中使用实际数据源的选项。

Some of the errors I am getting: 我遇到的一些错误:

Notice: Undefined index: prefix in lib/Cake/TestSuite/Fixture/CakeFixtureManager.php on line 169

Warning: in_array() expects parameter 2 to be array, null given in lib/Cake/TestSuite/Fixture/CakeFixtureManager.php on line 171

Warning: in_array() expects parameter 2 to be array, null given in lib/Cake/TestSuite/Fixture/CakeFixtureManager.php on line 174

Fatal error: Call to undefined method DataFeedSource::execute() in lib/Cake/Test/Suite/Fixture/CakeTestFixture.php on line 194

I am not sure if this is the best way to do it, but it works for me. 我不确定这是否是最好的方法,但这对我有用。 I have removed public $useDbConfig = 'test_datafeeds'; 我已经删除了公共$ useDbConfig ='test_datafeeds'; from the fixture and it works. 从固定装置开始工作。 It seems that just by having the test_datafeed configuration in my config file it works. 似乎只有在我的配置文件中包含test_datafeed配置,它才能起作用。

Quite handy as I can keep my static fixture as its and then toggle the test_datafeed on and off by commenting it out. 非常方便,因为我可以将我的静态装置保持不变,然后通过注释掉它来打开和关闭test_datafeed。

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

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