Laravel 如何将键值附加到factory()的结果中->;在拉威尔做什么?
我正在写一个测试用例 假设有一个线程模型,包含以下字段:用户id、标题、正文 因此,为了测试提交线程是否有效,我将执行以下操作:Laravel 如何将键值附加到factory()的结果中->;在拉威尔做什么?,laravel,laravel-5,Laravel,Laravel 5,我正在写一个测试用例 假设有一个线程模型,包含以下字段:用户id、标题、正文 因此,为了测试提交线程是否有效,我将执行以下操作: $user = factory('App\User')->create(); $thread = factory('App\Thread')->make(['user_id' => $user->id]); $this->post('/threads', $thread->toArray()); $this->get('/t
$user = factory('App\User')->create();
$thread = factory('App\Thread')->make(['user_id' => $user->id]);
$this->post('/threads', $thread->toArray());
$this->get('/thread/'.$thread->id)
->assertSee($thread->title)
但是我还有另外一个字段,我想和线程一起发布,它不是线程模型的一部分
例如
那么,在将数组发布到/threads
之前,如何附加另一个字段呢
如何将键值对附加到make()的结果?您可以使用以下函数:
$thread = factory('App\Thread')->make(['user_id' => $user->id]);
$payload = $thread->toArray();
$this->post('/threads', data_set($payload, 'community', 'some_community'));
$thread = factory('App\Thread')->make(['user_id' => $user->id]);
$payload = $thread->toArray();
$this->post('/threads', data_set($payload, 'community', 'some_community'));