测试一个调用parent :: function的类? [英] Testing a class that calls parent::function?

查看:85
本文介绍了测试一个调用parent :: function的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个重载where函数的模型.

I have a model that overloads the where function.

我的重载方法如下:

public function where($column, $operator = null, $value = null, $boolean = 'and')
{
    if (in_array($column, $this->metaFields))
    {
        $value    = '%"' . $column . '":"' . $value . '"';
        $column   = 'meta';
        $operator = 'like';
    }

    return parent::where($column, $operator, $value, $boolean);
}

现在使用phpunit和嘲笑我试图测试这个类,我需要测试重载的where函数,我真正关心的是传递给parent::where()

Now using phpunit and mockery I am trying to test this class, I need to test my overloaded where function, all I really care about is what the values are that get passed to parent::where()

我的问题是,是否可以/如何模拟父类以便我可以做

My question is, is it possible/how would I mock the parent class so I can do

$metaField = 'colour';
$value     = 'blue';
//on the parent
->shouldReceive('where')->with(['meta', 'like', '%"colour":"blue"%'])->once();

//on the model I am testing
$model->where('colour', 'blue');

推荐答案

我真正关心的是传递给parent :: where()

all I really care about is what the values are that get passed to parent::where()

不,你不知道.这是一个实现细节,您不需要在单元测试中进行任何操作.放松一下.

No, you don't. This is an implementation detail you do not need to care about at all with unit testing. Just relax.

这不是在开玩笑,但是,如果您的设计需要这样做,则您应该偏向于继承而不是继承,这样您就可以模拟并模仿"并注入它,从而测试这种期望.根据定义,模拟中的模拟的父对象是类,因此在这种情况下,父对象是具有重载方法的类,而不是父对象.您不能用嘲讽(而不是我知道的任何其他PHP模拟库)来嘲笑父母".

That was no joke, however if your design requires this, you should favor composition over inheritance so that you can actually mock that "parent" and inject it, therefore test such an expectation. By definition, the parent of a mock in mockery is the class, so the parent in this case is the class with the overloaded method, not it's parent. You can not mock "parents" with mockery (and not with any other PHP mock library I'm aware of).

这篇关于测试一个调用parent :: function的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆