微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何在PHPUnit中assertInstanceOf()?

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

<?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) {
  }
}

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

解决方法:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐