在似乎产生错误的那个函数之后,我在测试函数上得到了这个
Test tests/wpunit/AdminUtilitiesTest.php:testQueryDbForNewSelection
Unexpected incorrect usage notice for Mockery_0_wpdb
。
class AdminUtilitiesTest extends \Codeception\TestCase\WPTestCase {
public function tearDown() {
\Mockery::close();
// then
parent::tearDown();
}
// tests
public function testGlobals() {
global $wpdb;
// commenting the next 3 full statements fixes/hides the error
$wpdb = \Mockery
::mock('\WPDB')
->makePartial();
$wpdb
->shouldReceive('mockMethod')
->once()
->andReturn(23);
$this->assertEquals(23, $wpdb->mockMethod());
\Mockery::close(); // removing this extra call doesn't do anything
}
public function testQueryDbForNewSelection() {
$adminUtilitiesMock = \Mockery
::mock('AdminUtilities')
->makePartial();
$adminUtilitiesMock
->shouldReceive('loadTargets')
->andReturn(17);
$this->assertEquals(17, $adminUtilitiesMock::loadTargets());
}
}
// loadTargets() from the target file
public static function loadTargets() {
global $wpdb;
$query = $wpdb->get_results(
"select * from {$wpdb->prefix}fvc
limit {$_POST['limit']}
offset {$_POST['resultMarker']}
");
return $query;
}
测试隔离似乎有问题。我不认为Mockery :: close()在做什么。
我在Mockery文档中看到了这一点:
Mockery被设计为易于使用的独立模拟对象 框架,因此它与任何测试框架集成的需求是 完全可选。要集成Mockery,我们需要定义一个 我们的测试中的tearDown()方法包含以下内容(我们可以使用 \ Mockery名称空间别名更短):
public function tearDown() {
\Mockery::close();
}
也尝试过:
在global $wpdb
内设置testQueryDbForNewSelection()
。没有任何改变
答案 0 :(得分:0)
通过删除全局变量声明来解决:
public function testGlobals() {
// global $wpdb; // this line was removed
$wpdb = \Mockery
::mock('\WPDB')
->makePartial();
$wpdb
->shouldReceive('mockMethod')
->once()
->andReturn(23);
$this->assertEquals(23, $wpdb->mockMethod());
\Mockery::close();
}