duan5801 2016-02-02 06:10
浏览 40
已采纳

为什么不嘲笑日志记录调用?

So I am not sure what I am doing wrong but:

class Details {

    public function details($href) {
        $client = new Client();
        $eveLog = new EveLogHandler();

        $response = $client->request('GET', $href);
        $eveLog->requestLog($response, 'eveonline_item_details.log');

        if ($response->getStatusCode() === 200) {
            return json_decode($response->getBody()->getContents());
        }

        return false;
    }
}

I want to mock this: $eveLog->requestLog($response, 'eveonline_item_details.log');

So I wrote a test:

class DetailsTest extends \PHPUnit_Framework_TestCase {

    public function getLogMock() {
        return $this->getMockBuilder('EveOnline\Logging\EveLogHandler')
             ->setMethods(['requestLog'])
             ->getMock();
    }

    public function testDetailsDoesNotReturnFalse() {

        $details = new EveOnline\Items\Details();

        $logger = $this->getLogMock();
        $logger->expects($this->exactly(1))
            ->method('requestLog')
            ->with('request', 'request.log');

        $response = $details->details('http://example.com');

        $this->assertNotFalse($response);
    }
}

Accept it doesnt mock the method call, instead it freaks out with an error:

1) DetailsTest::testDetailsDoesNotReturnFalse
Error: Call to undefined function EveOnline\Logging\storage_path()

Now this method: storage_path is a laravel method and since this library is out side of laravel, for development purposes, the method doesn't exist, hence me trying to mock the call to: $eveLog->requestLog($response, 'eveonline_item_details.log');

Two questions arise from this:

  1. First of all why doesn't my test pick up on the fact that I have mocked the method call?
  2. When it coms time to test this particular method how do you mock global functions? Laravel has a few of them and I'll need mock out the storage_path method.

Ideas?

update

It should be known that this class gets turned into a laravel facade via a provider class and a facade class that both get registered with laravel allowing me to do something like

EveDetails::details()

For example. Learn more here https://laravel.com/docs/5.1/facades

I can post the facade and provider class of that will help.

The only reason the class is instantiated in my ear is because this particular component is being built as a separate installable component of laravel.

  • 写回答

1条回答 默认 最新

  • doutao5419 2016-02-02 06:31
    关注

    Your method Details::details() doesn't know that $eveLog should be an instance of your mock instead of class EveLogHandler. You have to inject the instance of your logger by using dependency injection.

    The simpliest way to do this is to put it to the constructor:

    class Details {
    
        private $eveLog;
    
        public function __construct($eveLog) {
            $this->eveLog = $eveLog;
        }
    
        public function details($href) {
            $client = new Client();
    
            $response = $client->request('GET', $href);
            $this->eveLog->requestLog($response, 'eveonline_item_details.log');
    
            if ($response->getStatusCode() === 200) {
                return json_decode($response->getBody()->getContents());
            }
    
            return false;
        }
    }
    

    In your production code you'll have to call:

    $details = new EveOnline\Items\Details(new EveLogHandler());
    

    to inject the real Eventlogger (an instance of class EveLogHandler) to the instance of class Details.

    In your TestCase you'll now have inject your mock:

    $logger = $this->getLogMock();
    $logger->expects($this->exactly(1))
           ->method('requestLog')
           ->with('request', 'request.log');
    
    $details = new EveOnline\Items\Details($logger);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应