我如何在PHPUnit中assertInstanceOf()?

时间:2016-06-24 20:21:36

标签: php unit-testing testing phpunit

我已经为单元测试编写了这段代码:

<?php

namespace tests;

use data\address\Address;
use lists\Collection;
use PHPUnit\Framework\TestCase;

class CollectionTest extends TestCase
{
    public function testIsCollectionOf()
    {
        $Collection = new Collection(Address::class);
        $this->assertInstanceOf($Collection, Collection::class);
    }
}

当我跑过来时,我遇到了这个错误:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.6-6+donate.sury.org~trusty+1
Configuration: /var/www/project/phpunit.xml

E                                                                   1 / 1 (100%)


Time: 33 ms, Memory: 2.00MB

There was 1 error:

1) tests\CollectionTest::testIsCollectionOf
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertInstanceOf() must be a class or interface name

/var/www/project/vendor/phpunit/phpunit/src/Util/InvalidArgumentHelper.php:30
/var/www/project/vendor/phpunit/phpunit/src/Framework/Assert.php:1323
/var/www/project/tests/CollectionTest.php:19
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:1081
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:932
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestResult.php:701
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:888
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:753
/var/www/project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:465
/var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:162
/var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:113

ERRORS!
Tests: 1, Assertions: 0, Errors: 1

现在,如果我var_dump($Collection),我得到了:

object(Collection)#18 (2) {
  ["class_name":"Collection":private]=>
  string(32) "data\address\Address"
  ["_items":protected]=>
  array(0) {
  }
}

为什么呢?我的测试用例有什么问题?

1 个答案:

答案 0 :(得分:7)

参数的顺序是错误的。试试这个:

$this->assertInstanceOf(Collection::class, $Collection);