0

我有一个帖子和一个博客类。

从下面可以看出, Posts 类依赖于 Blog 类。

public function index(Blog $blog) {
    $posts = $this->post->all()->where('blog_id', $blog->id)->orderBy('date')->paginate(20);
    return View::make($this->tmpl('index'), compact('blog', 'posts'));
}

此操作的 url 如下:

http://example.com/blogs/[blog_name]/posts

我正在尝试对此进行测试,但遇到了问题。

这是我的测试类 PostTestController:

public function setUp() {
    parent::setUp();
    $this->mock = Mockery::mock('Eloquent', 'Post');
}

public function tearDown() {
    Mockery::close();
}

public function testIndex() {

    $this->mock->shouldReceive('with')->once();

    $this->app->instance('Post', $this->mock);

    // get posts url
    $this->get('blogs/blog/posts'); //this is where I'm stuck.

    $this->assertViewHas('posts');
}

问题是……当 get 本身包含基于数据的变量输出时,如何测试 get 调用?如何正确测试?

4

1 回答 1

0

首先,您的代码中有错误。您可以删除 all()。

$posts = $this->post
  ->where('blog_id', $blog->id)
  ->orderBy('date')
  ->paginate(20);

其次,我不知道单元测试路由模型绑定的方法,所以我会更改public function index(Blog $blog)public function index($blogSlug)然后做$this->blog->where('slug', '=', $blogSlug)->first()一些类似的事情。

第三,只是做m::mock('Post'),放弃 Eloquent 位。如果您遇到此问题,请执行m::mock('Post')->makePartial().

如果您想绝对测试所有内容,这就是测试的大致样子。

use Mockery as m;

/** @test */
public function index()
{
    $this->app->instance('Blog', $mockBlog = m::mock('Blog'));
    $this->app->instance('Post', $mockPost = m::mock('Post'));
    $stubBlog = new Blog(); // could also be a mock
    $stubBlog->id = 5;
    $results = $this->app['paginator']->make([/* fake posts here? */], 30, 20);
    $mockBlog->shouldReceive('where')->with('slug', '=', 'test')->once()->andReturn($stubBlog);
    $mockPost->shouldReceive('where')->with('blog_id', '=', 5)->once()->andReturn(m::self())
        ->getMock()->shouldReceive('orderBy')->with('date')->once()->andReturn(m::self())
        ->getMock()->shouldReceive('paginate')->with(20)->once()->andReturn($results);

    $this->call('get', 'blogs/test/posts');
    // assertions
}

这是一个很好的例子,很难对与数据库层耦合的层进行单元测试(在这种情况下,您的 Blog 和 Post 模型是数据库层)。相反,我会建立一个测试数据库,用虚拟数据为其播种并在其上运行测试,或者将数据库逻辑提取到存储库类,将其注入控制器并模拟它而不是模型。

于 2014-05-22T12:51:16.000 回答