Laravel 打包测试

Laravel package test

大家好,我正在创建一个 Laravel 包,我正在尝试实施测试。

我的 composer.json 有这样的结构:

"require-dev": {
    "graham-campbell/testbench": "^3.1",
    "mockery/mockery": "^0.9.4",
    "phpunit/phpunit": "^4.8|^5.0"
},

我正在使用 this package 进行测试创建。我查看了 Graham Campbell 的其他软件包,以便更好地了解他是如何创建测试的,我正在尝试 "adapt" 他的 classes 以实现我的目标。

问题是我在 运行 phpunit:

时收到此错误
1) IlGala\Tests\LaravelWizzy\WizzyTest::testGetPrefix
Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_Illuminate_Contracts_Config_Repository::get("wizzy.prefix", ""). Either the method was unexpected or its arguments matched no expected argument list for this method

/home/ilgala/NetBeansProjects/laravelWizzy/packages/ilgala/laravel-wizzy/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:93
/home/ilgala/NetBeansProjects/laravelWizzy/packages/ilgala/laravel-wizzy/src/Wizzy.php:68
/home/ilgala/NetBeansProjects/laravelWizzy/packages/ilgala/laravel-wizzy/tests/WizzyTest.php:71

我正在尝试测试在 WizzyServiceProvider 中注册为单例的 Wizzy class:

$this->app->singleton('wizzy', function (Container $app) {
    $config = $app['config'];

    return new Wizzy($config);
});

这是我的测试class:

protected $defaults = [
    [...]
];

/**
 *
 */
public function testGetPrefix()
{
    $wizzy = $this->getWizzy();

    $this->assertSame('install', $wizzy->getPrefix());
}

protected function getWizzy()
{
    $repository = Mockery::mock(Repository::class);

    $wizzy = new Wizzy($repository);

    $wizzy->getConfig()->shouldReceive('get')->once()
            ->with('wizzy.prefix')->andReturn($this->defaults['prefix']);

    return $wizzy;
}

最后这是 Wizzy class:

/**
 * Config repository.
 *
 * @var \Illuminate\Contracts\Config\Repository
 */
protected $config;

/**
 * Creates new instance.
 */
public function __construct(Repository $config)
{
    $this->config = $config;
}

/**
 * Get the config instance.
 *
 * @return \Illuminate\Contracts\Config\Repository
 */
public function getConfig()
{
    return $this->config;
}

/**
 * Get the configuration name.
 *
 * @return string
 */
protected function getConfigName()
{
    return 'wizzy';
}

/**
 * Get wizzy route group prefix from the config file.
 *
 * @return string wizzy.prefix
 */
public function getPrefix()
{
    return $this->config->get($this->getConfigName() . '.prefix', '');
}

任何人都可以帮助我了解我做错了什么吗?

我找到了解决方案...

$wizzy->getConfig()->shouldReceive('get')->once()
        ->with('wizzy.prefix')->andReturn($this->defaults['prefix']);

问题就在这里,因为(用自然语言翻译这个方法)模拟对象应该接收到对带有 wizzy.prefix 字符串的 get 方法的调用,但实际上它接收到 wizzy.prefix'',所以我把代码改成了这样:

$wizzy->getConfig()->shouldReceive('get')->once()
        ->with('wizzy.prefix', '')->andReturn($this->defaults['prefix']);