2

我是测试新手,我正在尝试创建一个单元测试,涵盖 NewsCreator create 方法中的第一个 if 语句。

这个问题有两个部分。

第一:我应该如何实例化 NewsCreator 来处理模拟的验证器和存储库?

第二:测试这条路径的正确方法是什么?

这是我的控制器方法,它调用需要测试的类:

public function store()
{
    $creator = new NewsCreator($this);
    return $creator->create(Input::all());
}

这是我想测试的类,NewsCreator:

<?php namespace WHS\Portal\News;

class NewsCreator {

    protected $listener;
    protected $repository;
    protected $validator;
    protected $errors;

    public function __construct($listener, NewsRepositoryInterface $repository, NewsValidator $validator)
    {
        $this->listener = $listener;
        $this->repository = $repository;
        $this->validator = $validator;
        $this->errors = [];
    }
    public function create($data)
    {
        if($this->validator->fails($data))
        {
            return $this->listener->newsCreationFails($this->validator->messages());
        }
        if($this->repository->create($data))
        {
            return $this->listener->newsCreationSucceeds();
        }
        return $this->listener->newsCreationFails($this->errors);
    }
}

这是我尝试编写的测试,但失败并出现异常:

2) WHS\Portal\Tests\News\NewsCreatorTest::test_failed_validation Mockery\Exception\InvalidCountException: Method fails("foo") from Mockery_1_WHS_Portal_News_NewsValidator should be called exactly 1 times but called 0 times.

<?php namespace WHS\Portal\Tests\News;

    use TestCase;
    use Mockery as m;

    class NewsCreatorTest extends TestCase {

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

        public function test_failed_validation()
        {
            $newsRepo = m::mock('\WHS\Portal\News\DbNewsRepository["create"]');
            $newsValidator = m::mock('\WHS\Portal\News\NewsValidator["fails"]');

            $listener = new NewsListenerStub();
            $listener = m::mock($listener)->makePartial();

            $newsValidator->shouldReceive('fails')->with('foo')->once()->andReturn(true);
            $listener->shouldReceive('newsCreationFails')->once()->with('foo')->andReturn();

            $newsCreator = new \WHS\Portal\News\NewsCreator($listener,$newsRepo,$newsValidator);
            $newsCreator->create([]);
        }
    }

更新测试:

use TestCase;
use Mockery as m;

class NewsCreatorTest extends TestCase {

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

    public function test_failed_validation()
    {
        $newsRepo = m::mock('\WHS\Portal\News\DbNewsRepository["create"]');
        $newsValidator = m::mock('\WHS\Portal\News\NewsValidator["fails"]');

        $listener = m::mock('\WHS\Portal\Tests\News\NewsListenerStub["newsCreationFails"]');

        $newsValidator->shouldReceive('fails')->with([])->once()->andReturn(true);
        $newsValidator->shouldReceive('messages')->once();
        $listener->shouldReceive('newsCreationFails')->once()->with('foo')->andReturn('foo-bar');

        $newsCreator = new \WHS\Portal\News\NewsCreator($listener,$newsRepo,$newsValidator);

        $result = $newsCreator->create([]);
        $this->assertEquals('foo-bar', $result);
    }
}

存根类:

class NewsListenerStub
{
    public function newsCreationFails($data)
    {
        return $data;
    }
}

请帮忙。

谢谢。

4

1 回答 1

1

使用类中的参数fails()调用该方法。$data在您的单元测试中,您将一个空数组作为 data 传递create([])。您对 validatorMock 的参数期望期望fails()使用参数调用foo。您必须更改它以匹配空数组。

$newsValidator->shouldReceive('fails')->with([])->once()->andReturn(true);

此外,您还必须validator->messages()在 validatorMock 上指定方法,因为您的类中也会调用该方法。

$newsValidator->shouldReceive('messages')->once();

要使这个测试真正有意义,您必须断言 的结果与 .NewsCreationFails的返回值匹配create()

$listener->shouldReceive('newsCreationFails')->once()->with('foo')->andReturn('foo-bar');
...
$result = $newsCreator->create([]);

$this->assertEquals('foo-bar', $result);
于 2014-02-16T09:37:54.983 回答