PHPUnit测试错误:类(...)的对象无法转换为字符串

时间:2016-03-31 19:34:27

标签: php phpunit vfs

首先,我是PHPUnit测试和PHP的新手,对不起,如果我错过了太明显的东西。

好的,现在,对于我的问题:我正在使用名为ItemClickSupport.java的虚拟文件系统来测试函数VfsStream。出于某种原因,在我的测试中发生了这个错误:

04-01 16:28:47.241 32176-32176/com.example.omkar D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
04-01 16:28:47.371 32176-32176/com.example.omkar I/Timeline: Timeline: Activity_launch_request id:com.example.omkar time:77715999
04-01 16:28:47.381 32176-32176/com.example.omkar W/ResourcesManager: getTopLevelResources: com.example.omkar for user  0
04-01 16:28:47.391 32176-32176/com.example.omkar W/ResourcesManager: getTopLevelResources: com.example.omkar for user  0
04-01 16:28:47.401 32176-32176/com.example.omkar D/PhoneWindow: *FMB* installDecor mIsFloating : false
04-01 16:28:47.401 32176-32176/com.example.omkar D/PhoneWindow: *FMB* installDecor flags : -2139029248
04-01 16:28:47.431 32176-32176/com.example.omkar D/Activity: performCreate Call Injection manager
04-01 16:28:47.431 32176-32176/com.example.omkar I/InjectionManager: dispatchOnViewCreated > Target : com.example.omkar.userInterface.activity.product.ProductActivity isFragment :false
04-01 16:28:47.431 32176-32176/com.example.omkar D/DisplayManager: DisplayManager()
04-01 16:28:47.461 32176-32176/com.example.omkar D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
04-01 16:28:47.461 32176-32176/com.example.omkar D/PhoneWindow: *FMB* isFloatingMenuEnabled return false
04-01 16:28:47.491 32176-32176/com.example.omkar W/View: View too large to fit into drawing cache, needs 10563840 bytes, only 3686400 available
04-01 16:28:47.501 32176-32176/com.example.omkar W/View: View too large to fit into drawing cache, needs 10563840 bytes, only 3686400 available
04-01 16:28:47.511 32176-32176/com.example.omkar I/InjectionManager: dispatchCreateOptionsMenu :com.example.omkar.userInterface.activity.product.ProductActivity
04-01 16:28:47.511 32176-32176/com.example.omkar I/InjectionManager: dispatchPrepareOptionsMenu :com.example.omkar.userInterface.activity.product.ProductActivity
04-01 16:28:47.531 32176-32176/com.example.omkar W/View: View too large to fit into drawing cache, needs 10563840 bytes, only 3686400 available
04-01 16:28:47.561 32176-32176/com.example.omkar I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1d12fb86 time:77716194
04-01 16:28:47.921 32176-32176/com.example.omkar W/View: View too large to fit into drawing cache, needs 10563840 bytes, only 3686400 available
04-01 16:28:47.961 32176-32191/com.example.omkar I/art: Background sticky concurrent mark sweep GC freed 32383(1734KB) AllocSpace objects, 7(112KB) LOS objects, 9% free, 16MB/18MB, paused 5.876ms total 38.048ms

我知道我的Unlink课程和它返回的内容是什么,但我不知道是什么。

我正在测试的课程:     

[bianca@cr-22ncg22 tests-phpunit]$ phpunit
PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

Configuration read from /var/www/html/tests-phpunit/phpunit.xml

E

Time: 27 ms, Memory: 4.50Mb

There was 1 error:

1) test\UnlinkTest\UnlinkTest::testUnlink
Object of class App\Libraries\Unlink could not be converted to string

/var/www/html/tests-phpunit/test/UnlinkTest.php:21
/home/bianca/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:153
/home/bianca/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:105

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

我的测试所在的课程:     

class Unlink {

    public function unlinkFile($file) {

        if (!unlink($file)) {
            echo ("Error deleting $file");
        }
        else {
            echo ("Deleted $file");
        }

        return unlink($file);
    }

}

?>

有人可以帮我解决吗?

1 个答案:

答案 0 :(得分:2)

你得到的错误是因为最初你创建了这个局部变量:

$removeFile = new Unlink();

但是当你这样做时,你会将其称为$this->$removeFile

$this->$removeFile->unlinkFile(vfsStream::url('home/test.txt'));

这不正确;您可以使用具有类变量的地方,并且您想要动态引用它。 e.g。

class YourClass {
    public $foo;
    public $bar;

    public function __construct() {
        $this->foo = 'hello';
        $this->bar = 'world';
    }

    public function doStuff() {
        $someVariable = 'foo';

        echo $this->$someVariable;  // outputs 'hello'
    }
}

您需要做的就是摆脱$this,并将其更改为:

$removeFile->unlinkFile(vfsStream::url('home/test.txt'));