我有以下测试:
public function testTestNameRequired(){
Validator::shouldReceive("make")->once()
->with(array(1,2,3), hasKeyValuePair("name",array("required")))
->andReturn(m::mock(["passes"=>true]));
$r = $this->vendorRepo->test(array(1,2,3));
assertThat($r,is(TRUE));
}
当我运行此测试时,我收到以下错误:
1) VendorRepoTest::testTestNameRequired
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Illuminate\Validation\Factory::make(Array, Array). Either the method was unexpected or its arguments matched no expected argument list for this method
Hamcrest 显然已安装,因为测试不会抱怨不知道是什么hasKeyValuePair()
。但我想它没有做我认为应该做的事情。我重写了它以使用 Hamcrest 的anything()
方法,如下所示:
public function _testTestFieldRequired($field){
Validator::shouldReceive("make")->once()
->with(array(1,2,3), anything()) //hasKeyValuePair($field,array("required")))
->andReturn(m::mock(["passes"=>true]));
$r = $this->vendorRepo->test(array(1,2,3));
assertThat($r,is(TRUE));
}
我得到同样的错误。但是,使用Mockery::any()
作品:
public function _testTestFieldRequired($field){
Validator::shouldReceive("make")->once()
->with(array(1,2,3), Mockery::any()) //hasKeyValuePair($field,array("required")))
->andReturn(m::mock(["passes"=>true]));
$r = $this->vendorRepo->test(array(1,2,3));
assertThat($r,is(TRUE));
}
我在想我没有正确安装 Mockery 或 Hamcrest。他们在一起打得不好。这是我的composer.json:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"require": {
"laravel/framework": "4.0.*",
"way/generators": "dev-master",
"zurb/foundation": "v4.3.1",
"intervention/image": "dev-master",
"intervention/helper": "dev-master"
},
"require-dev": {
"mockery/mockery": "dev-master@dev",
"cordoval/hamcrest-php": "dev-master@dev",
"way/laravel-test-helpers": "dev-master",
"loic-sharma/profiler": "dev-master"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/OfficePilot"
],
"files": [
"vendor/cordoval/hamcrest-php/hamcrest/Hamcrest.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}
知道发生了什么吗?