0

我有以下测试:

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"
}

知道发生了什么吗?

4

2 回答 2

0

我遇到了同样的问题,它似乎与 cordoval/hamcrest-php 包隔离。

我已经切换到 davedevelopment/hamcrest-php 包来解决。

于 2013-11-29T03:45:02.167 回答
0

现在存储库已正式移至https://packagist.org/packages/hamcrest/hamcrest-php。您可以使用 v1.0.0 作为依赖项来兼容最新的 Mockery 版本。

有一个 PR https://github.com/padraic/mockery/pull/170更新了 Mockery 代码以与 Hamcrest v1.1.0 版本兼容,即使用命名空间而不是全局匹配器函数。

于 2013-12-31T12:14:04.357 回答