Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用抽象参数实例化对象 接口接口{ 公共函数构造(类抽象$a,类抽象$b){ } } 类示例实现了aInterface { 公共函数构造(类抽象$a,类抽象$b){ //实施 $this->init(); } 公共函数init(){ //实施 } }_Php_Unit Testing_Oop_Automated Tests_Phpunit - Fatal编程技术网

Php 用抽象参数实例化对象 接口接口{ 公共函数构造(类抽象$a,类抽象$b){ } } 类示例实现了aInterface { 公共函数构造(类抽象$a,类抽象$b){ //实施 $this->init(); } 公共函数init(){ //实施 } }

Php 用抽象参数实例化对象 接口接口{ 公共函数构造(类抽象$a,类抽象$b){ } } 类示例实现了aInterface { 公共函数构造(类抽象$a,类抽象$b){ //实施 $this->init(); } 公共函数init(){ //实施 } },php,unit-testing,oop,automated-tests,phpunit,Php,Unit Testing,Oop,Automated Tests,Phpunit,如何在使用PHPUnit进行测试时设置此选项 …实施。。。 试验 ... 函数设置(){ //初始化 } 您可能正在寻找模拟对象 假设您要测试样本,您将使用: 引述: getMockForAbstractClass()方法返回抽象类的模拟对象。给定抽象类的所有抽象方法都是模拟的。这允许测试抽象类的具体方法 有关测试双精度的更多信息,请参见该章。简而言之,使用创建需要传入的类的集合,以便测试它们 class SampleTest extends PHPUnit_Framework_TestCas

如何在使用PHPUnit进行测试时设置此选项

…实施。。。 试验

... 函数设置(){ //初始化 }
您可能正在寻找模拟对象


假设您要测试
样本
,您将使用:

引述:

getMockForAbstractClass()
方法返回抽象类的模拟对象。给定抽象类的所有抽象方法都是模拟的。这允许测试抽象类的具体方法

有关测试双精度的更多信息,请参见该章。简而言之,使用创建需要传入的类的集合,以便测试它们

class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        $stubA = $this->getMockForAbstractClass('aClass_Abstract');
        $stubA->expects($this->any())
              ->method('abstractMethod')
              ->will($this->returnValue(TRUE));

        // do the same for bClass_Abstract

        $sample = new Sample($stubA, $stubB);
        // add an assertion for sample
    }
}
完整代码示例 (修复了接口定义)


谢谢你的回答。这对我帮助很大,因为我是单元测试新手。
...
function setUp(){
    //initialize
}
class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testSomething()
    {
        $stubA = $this->getMockForAbstractClass('aClass_Abstract');
        $stubA->expects($this->any())
              ->method('abstractMethod')
              ->will($this->returnValue(TRUE));

        // do the same for bClass_Abstract

        $sample = new Sample($stubA, $stubB);
        // add an assertion for sample
    }
}
$a = $this->getMockForAbstractClass("aClass_Abstract");
$b = $this->getMockForAbstractClass("bClass_Abstract");
$class = new Sample($a, $b);
<?php

interface aInterface{
     public function __construct(aClass_Abstract $a, bClass_Abstract $b);
}
class Sample implements aInterface
{
     public function __construct(aClass_Abstract $a, bClass_Abstract $b){
         //implementation
         $this->a = $a;
     }
     public function init(){
         return $this->a->myMethod();
     }
}

abstract class aClass_Abstract {
    abstract public function myMethod();
}

abstract class bClass_Abstract {}

class SampleTest extends PHPUnit_Framework_TestCase {

    public function testSetup() {

        $a = $this->getMockForAbstractClass("aClass_Abstract");
        $a->expects($this->once())->method("myMethod")->will($this->returnValue(true));
        $b = $this->getMockForAbstractClass("bClass_Abstract");
        $class = new Sample($a, $b);
        $this->assertTrue($class->init());

    }
}


/*
PHPUnit 3.5.12 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 3.25Mb
*/