Laravel package test


Laravel package test

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

我的作曲家。Json有这样的结构:

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

我使用这个包来创建测试。为了更好地理解他是如何创建测试的,我已经查看了Graham Campbell的其他包,并且我正在尝试"改编"他的类来实现我的目标。

问题是当我运行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类:

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

这是我的测试类:

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类:

/**
 * 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']);