【问题标题】:Laravel 4.1 PHPUnit Test: Call to a member function with() on a non-objectLaravel 4.1 PHPUnit 测试:在非对象上调用成员函数 with()
【发布时间】:2014-02-21 09:17:43
【问题描述】:

嗨,我正在尝试编写 PHPUnit 测试来测试我的登录控制器,我的代码似乎在浏览器中运行良好,但是当我运行 php 单元测试时,我收到以下错误

错误:

Call to a member function with() on a non-object

方法:

public function store()
{
    $input = Input::only('email', 'password');
    $attempt = Auth::attempt($input);

    if($attempt){
        return Redirect::intended('/')
            ->with('flash_message', Lang::get('sessions.you_have_logged_in'));
    }
}

单元测试:

public function testStore()
{
    Auth::shouldReceive('attempt')
            ->once()
            ->andReturn(true);

    Redirect::shouldReceive('intended')
            ->once();

    $this->call('POST', 'session');
}

在使用嘲弄方面我有点菜鸟,所以我想问题可能在于我如何模拟重定向对象?

【问题讨论】:

    标签: php unit-testing laravel-4 phpunit mockery


    【解决方案1】:

    你需要从 Redirect::shouldReceive(...) 返回一些东西

    public function testStore()
    {
        Auth::shouldReceive('attempt')
            ->once()
            ->andReturn(true);
    
        Redirect::shouldReceive('intended')
            ->once()
            ->andReturn($redirectResponse = Mockery::mock('Illuminate\Http\RedirectResponse'));
    
        $this->call('POST', 'session');
    }
    

    重定向正在正确接收“预期”,但根据实际函数,它返回一些东西(特别是 Illuminate\Http\RedirectResponse 的实例)

    您可以查看 Illuminate\Routing\Redirector 以获得更多信息。

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 2014-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      相关资源
      最近更新 更多