【发布时间】:2014-11-07 21:06:39
【问题描述】:
我正在尝试测试这个控制器方法。
public function handleCreate()
{
$offer = new Offer();
$offer->key_word = $key_word;
$offer->url = $url;
$offer->save();
return Redirect::action('OffersController@index');
}
这是测试。
public function testCreate(){
Input::replace($input = array('key_word' => 'foo', 'url' => 'http://bar.com'));
$this->mock->shouldReceive('create')->once()->with($input);
$this->app->instance('Offer', $this->mock);
$this->call('POST', 'create');
$this->assertRedirectedToRoute('/');
}
我在运行测试时收到此错误。
PHP 致命错误:调用未定义的方法 stdClass::isEmpty()
这是视图中搞砸的部分。
@if($offers->isEmpty())
<p>No offers found.</p>
@else
...
@endif
我不知道如何将 isEmpty() 方法添加到我的模拟模型中以克服此错误。
我试过用这个来模拟 isEmpty() 方法,但它仍然给出了同样的错误。
$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);
【问题讨论】:
-
您似乎没有
isEmpty()方法或类,这就是您出现错误的原因。 -
isEmpty() 方法是我的模拟对象正在模拟的 Eloquent 集合类的一部分。
标签: php laravel phpunit mockery