使用Hamcrest具有键值对与laravel单元测试

时间:2013-08-16 16:07:28

标签: php laravel laravel-4 hamcrest mockery

我有以下测试:

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

知道发生了什么事吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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版本兼容,即使用命名空间而不是全局匹配器函数。