相关疑难解决方法(0)

如何在功能测试中模拟Symfony 2服务?

我有symfony服务,它在某些方法中使用redis连接,但在所有方法中都没有.

class ServiceA
{
    private $redis;

    public function __construct($redis)
    {
        $this->redis = $redis;
    }

    public function getRequest($param1, $param2)
    {
    $result = $param1+ $param2;
        return $request;
    }

    .. other methods which use $redis connection
}
Run Code Online (Sandbox Code Playgroud)

我正在为仅使用getRequest方法的代码编写功能测试(此方法不需要redis连接),但是当构造函数将连接作为参数时,当我运行test时,它尝试连接redis服务器.

如何编写完全不使用redis连接的模拟服务并忽略原始构造函数.

我正在尝试下面提到的方法,但没有成功.我仍然尝试连接redis,尽管我已经禁用了原始构造函数.

http://blog.lyrixx.info/2013/04/12/symfony2-how-to-mock-services-during-functional-tests.html

$serviceA = $this->getMockBuilder('ServiceA')
    ->disableOriginalConstructor()
    ->getMock();

static::$kernel->getContainer()->set('my_bundle.service.a', $serviceA);
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mocking symfony

28
推荐指数
2
解决办法
3万
查看次数

标签 统计

mocking ×1

php ×1

phpunit ×1

symfony ×1

unit-testing ×1