【发布时间】:2015-11-06 02:23:58
【问题描述】:
我正在尝试为一个函数编写一个单元测试,该函数会立即从另一个类中加载一个对象,该类将函数的输入用作参数。我是 php 单元测试的新手,找不到任何可以解决我的特定问题的东西。我得到的一些导致无济于事的线索是使用注射器,并试图让我们反思。
我要为其编写单元测试的代码是:
public static function isUseful($item) {
$objPromo = MyPromoCodes::Load($item->SavedSku);
if (!is_null($objPromo)
&& ($objPromo->PromoType == MyPromoCodes::Interesting_Promo_Type)) {
return true;
}
return false;
}
我试图嘲笑这个:
public function testIsUseful() {
$injector = $this->getMockBuilder('MyPromoCodes')
->setMethods(array('Load'))
->getMock();
$objPromo = $this->getMock('MyPromoCodes');
$objPromo->PromoType = 'very interesting promo type';
$injector->set($objPromo, 'MyPromoCodes');
$lineItem1 = $this->getDBMock('LineItem');
$this->assertTrue(MyClass::isUseful($lineItem1));
}
但是这不起作用,因为该对象没有设置方法....
不知道还有什么可以尝试的,任何帮助将不胜感激。
【问题讨论】:
标签: php unit-testing testing phpunit