简体   繁体   English

创建PHPUnit模拟模型使用默认数据库而不是CakePHP中的test

[英]Creating a PHPUnit mocked model uses default database instead of test in CakePHP

I am testing a model in CakePHP 2 and have mocked a model like this: 我在CakePHP 2中测试一个模型并且模拟了这样的模型:

$this->User = $this->getMock('User', array(
  '_saveUploadedFile',
  '_removeUploadedFile',
)); 
$this->User->expects($this->any())
        ->method('_saveUploadedFile')
        ->with($data, Configure::read('App.myDirectory'), true);
        ->will($this->returnValue(true));
$this->User->expects($this->any())
        ->method('_removeUploadedFile')
        ->with($data, Configure::read('App.myDirectory'))
        ->will($this->returnValue(true));

Since any operation with the database raises the following error: 由于使用数据库的任何操作都会引发以下错误:

"Database table mock__user_b6241a4cs for model User was not found."

I redefined the model information: 我重新定义了模型信息:

$this->User->alias = 'User';
$this->User->useTable = 'users';

Now the test works well, but it's using the $default database in database.php instead of $test . 现在测试运行良好,但它使用database.php中的$default database.php而不是$test What can be happening? 可能发生什么?

Why the database configuration for testing changes when using mocked objects? 使用模拟对象时,为什么测试的数据库配置会发生变化? Could it be related to database permissions that causes the mocked object not being able to create its custom tables? 是否与数据库权限相关,导致模拟对象无法创建自定义表?

Thanks! 谢谢!

I finally solved the problem by passing the correct parameters to the model constructor in getMock() third agument: 我终于通过将正确的参数传递给getMock()第三个文章中的模型构造函数来解决问题:

$this->User = $this->getMock('User',
  array('_saveUploadedFile', '_removeUploadedFile'),
  array(false, 'users', 'test')
);

Applying what is said in Stubs section of PHPUnit documentation, this third argument indicates that I want to use the users table and the test datasource. 应用PHPUnit文档的Stubs部分中的内容,第三个参数表明我想使用users表和test数据源。

I have to keep the redefinition of the alias property though: 我必须保留alias属性的重新定义:

$this->User->alias = 'User';

because a simple $this->User->read(null, 1) raises an error saying that 'User.a_column' couldn't be found. 因为一个简单的$this->User->read(null, 1)引发一个错误,指出找不到'User.a_column'

Thanks to José Lorenzo . 感谢JoséLorenzo

I haven't encountered this specific problem before, but I'm curious if you've tried setting the specific connection you want. 我之前没有遇到过这个特定的问题,但是我很好奇你是否尝试过设置你想要的特定连接。 So the same way you set alias and useTable : 所以你设置aliasuseTable方式相同:

$this->User->useDbConfig = 'test';

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

相关问题 通过同时使用POST和GET数据的phpunit进行Cakephp测试 - Cakephp test via phpunit that uses both POST and GET data Cakephp测试数据库-模型问题 - Cakephp test database - Problems with a model 如何使用phpunit此类进行测试,该类使用与数据库的连接 - How to test with phpunit this class, which uses connections to the database CakePHP:控制器使用其他模型而不是自己的模型 - CakePHP: controller uses another model instead of its own Codeception测试采用“默认”数据源,而不是CakePHP 3中的“测试” - Codeception Tests take 'default' datasource instead of 'test' in CakePHP 3 在模拟命名空间和原始命名空间中创建模拟 phpunit 差异 - creating mock phpunit discrepency in mocked namespace and the original one PHPUnit:在一次测试中对模拟方法使用多个断言是不好的做法? - PHPUnit: Is it a bad practice to use multiple assertions on mocked methods in one test? phpunit与嘲讽-如何测试带有适当参数的嘲笑对象 - phpunit with mockery - how to test a mocked object is constructed with proper arguments phpUnit测试-不存在非模拟方法 - PhpUnit test - Non-mocked method does not exist 从模拟模型的关系中获取属性(Laravel,PHPUnit) - Getting a property from a mocked model's relationship (Laravel, PHPUnit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM