第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

PhpUnit - 未調(diào)用模擬方法

PhpUnit - 未調(diào)用模擬方法

PHP
Helenr 2023-08-26 17:31:43
所以我有一個(gè)想要進(jìn)行組件測(cè)試的方法,但我無法模擬注入類的已使用方法:測(cè)試方法class PageEventHandler{    public const PAGE_TYPE_PRODUCT_LIST = 'product_list';    ...    private $pagePublishValidator;    public function __construct(        ...        PagePublishValidator $pagePublishValidator    ) {        ...        $this->pagePublishValidator = $pagePublishValidator;    }    public function preUpdate(AbstractObject $object)    {        $this->pagePublishValidator->validate($object);    }}在publishValidator類中,我有一個(gè)我想模擬的方法getPrevious,它是一個(gè)trait的方法GetPrevious.php。publishValidator 類如下所示:驗(yàn)證注入類的方法    public function validate(Page $object)    {        /** @var Page $previous */        $previous = $this->getPrevious($object);        var_dump('-----------------'); // <-- goes into here        if (!$previous) {            // a newly created object has no children            return;        }        var_dump('+++++++++++++++++++'); // <-- Does not go into here        var_dump('should go into here');    }測(cè)試用例public function testPreUpdateWithChildPageAndNewParent(){    $rootPage = $this->buildPage('', 'root');    $trait = $this->getMockBuilder(GetPrevious::class)        ->setMethods(['getPrevious'])        ->disableOriginalConstructor()        ->getMockForTrait();    $trait->expects($this->once())        ->method('getPrevious')        ->with($rootPage)        ->willReturn($rootPage); //Method called 0 times instead of one time, so mock seems to be wrong    $handler = new PageEventHandler(        $this->createAssertingMockProducer([], 0),        new NullProducer(),        new PagePublishValidator([PageEventHandler::PAGE_TYPE_PRODUCT_LIST])    );    $handler->preUpdate($rootPage);}
查看完整描述

1 回答

?
三國紛爭

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

的目的getMockForTrait是獨(dú)立測(cè)試特征。您必須模擬該方法PagePublishValidator

public function testPreUpdateWithChildPageAndNewParent()

{

? ? $rootPage = $this->buildPage('', 'root');


? ? $validator = $this->getMockBuilder(PagePublishValidator::class)

? ? ? ? ->setMethods(['getPrevious'])

? ? ? ? ->setConstructorArgs([PageEventHandler::PAGE_TYPE_PRODUCT_LIST])

? ? ? ? ->getMock();


? ? $validator->expects($this->once())

? ? ? ? ->method('getPrevious')

? ? ? ? ->with($rootPage)

? ? ? ? ->willReturn($rootPage);


? ? $handler = new PageEventHandler(

? ? ? ? $this->createAssertingMockProducer([], 0),

? ? ? ? new NullProducer(),

? ? ? ? $validator

? ? );


? ? $handler->preUpdate($rootPage);

}


查看完整回答
反對(duì) 回復(fù) 2023-08-26
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)