php 单元测试 phpunit ,如何mock类调用自身的方法?

WBOY
Release: 2016-06-02 11:31:24
Original
1909 people have browsed it

phpunit单元测试phplaravel5

使用laravel5做单元测试时,碰到以下问题,需要测试querySessionIdTest这个方法,
但是需要mock出sessionidtest这个方法,具体代码如下:
类如下:

<code>class ZabbixSystem implements ApmSystemInterface      public function querySessionIdTest()    {        echo 'querySessionIdTest';        return $this->sessionidtest();    }    public function sessionidtest()    {        echo 'sessionidtest';        return 'aaa';    }}</code>
Copy after login

单元测试方法:

<code>public function testQuerySessionIdSuccess()    {        $return = 'aaa';        $stub = Mockery::mock('mcokname');//        $this->app->instance('ZabbixSystem', $stub);        $stub->shouldReceive('querySessionIdTest')->andReturn($return);        $response = $stub->querySessionIdTest();        $this->assertEquals('aaa', $response);    }    public function testQuerySession()    {        // 为 SomeClass 类创建桩件。        $stub = $this->getMockBuilder('App\Services\ZabbixSystem')->setMethods(array(            'querySessionIdTest',            'sessionidtest'        ))->getMock();        // 配置桩件。        $stub->method('sessionidtest')->willReturn('111');        $this->app->instance('ZabbixSystem', $stub);        $this->assertEquals('foo', $stub->querySessionIdTest());    }</code>
Copy after login

通过两种方式进行测试,但都不成功,我希望的结果为:
echo 'querySessionIdTest';
然后测试成功

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!