如何模拟laravel雄辩的模型

时间:2014-01-04 19:10:03

标签: unit-testing laravel mocking laravel-4 phpunit

我一直在摸索这个 - 如何在Laravel 4中模拟一个扩展为Eloquent的模型进行单元测试?

我一直以当前的方式收到以下错误

ErrorException: Trying to get property of non-object

示例

use \Repository\Text\EloquentText;
use \Faker\Factory as Faker;

class EloquentTextTest extends TestCase {

public function setUp()
{
    parent::setUp();

    $stub = $this->getMock('Text');
    $stub->expects($this->any())->method('save');

    $this->_fixture = new EloquentText($stub);
}

/**
 * @test
 */
public function createShouldCreateNewTextEntry()
{
    $faker = Faker::Create();
    $data = [
        'title' => $faker->sentence,
        'content' => $faker->text,
        'level_id' => $faker->randomDigit,
        'is_public' => $faker->numberBetween(0, 1),
        'is_visible' => $faker->numberBetween(0, 1),
    ];

    $text = $this->_fixture->create($data);

    $this->assertEquals($data['title'], $text->title);
    $this->assertEquals($data['content'], $text->content);
    $this->assertEquals($data['level_id'], $text->level_id);
    $this->assertEquals($data['is_public'], $text->is_public);
    $this->assertEquals($data['is_visible'], $text->is_visible);

    return $text;
}   

1 个答案:

答案 0 :(得分:-1)

来自:' https://phpunit.de/manual/current/en/writing-tests-for-phpunit.html' The tests are public methods that are named test*. 在我的项目中,如果"测试"不是函数的前4个字母,测试不运行。 这至少是你问题的一部分吗? 异常发生在哪一行?