如何在拉拉维尔中将键值附加到工厂()->make() 的结果中?



我正在编写一个测试用例。

假设有一个线程模型,包含字段:user_id、title、body

因此,为了测试提交线程是否有效,我正在这样做:

$user = factory('AppUser')->create();
$thread = factory('AppThread')->make(['user_id' => $user->id]);
$this->post('/threads', $thread->toArray());
$this->get('/thread/'.$thread->id)
->assertSee($thread->title)

但我还有另一个字段,我想和线程一起发布,它不是线程模型的一部分。

例如。

community => 'some_community'

那么,在将数组发布到/threads之前,如何附加另一个字段呢。

如何将键值对附加到make((的结果

您可以使用data_set函数:

$thread = factory('AppThread')->make(['user_id' => $user->id]);
$payload = $thread->toArray();
$this->post('/threads', data_set($payload, 'community', 'some_community'));

最新更新