在PHPUnit中,如何模拟父方法? [英] In PHPUnit, how do I mock parent methods?

查看:77
本文介绍了在PHPUnit中,如何模拟父方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一个类方法,该方法调用具有相同名称的父方法.有办法吗?

I want to test a class method that calls upon a parent method with the same name. Is there a way to do this?

class Parent {

    function foo() {
        echo 'bar';
    }
}

class Child {

    function foo() {
            $foo = parent::foo();
            return $foo;
    }
}

class ChildTest extend PHPUnit_TestCase {

    function testFoo() {
        $mock = $this->getMock('Child', array('foo'));

        //how do i mock parent methods and simulate responses?
    }
}

推荐答案

您不要在被测对象(SUT)中模拟或存根方法.如果您认为有必要在SUT的父级中模拟或存根方法,则可能意味着您不应该使用继承,而应该使用聚合.

You dont mock or stub methods in the Subject-under-Test (SUT). If you feel you have the need to mock or stub a method in the parent of the SUT, it likely means you shouldnt have used inheritance, but aggregation.

您嘲笑被测对象的依赖项.这意味着SUT需要工作的任何其他对象.

You mock dependencies of the Subject-under-Test. That means any other objects the SUT requires to do work.

这篇关于在PHPUnit中,如何模拟父方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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