【问题标题】:PHPUnit and Laravel: Call to undefined method stdClass::isEmpty()PHPUnit 和 Laravel:调用未定义的方法 stdClass::isEmpty()
【发布时间】: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


【解决方案1】:

似乎我的问题的答案是

$this->mock->shouldReceive('isEmpty')->once()->andReturn(false);

但我猜我第一次尝试时设置错误。

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多