在Laravel中测试时充当用户的ID

时间:2019-02-27 13:23:53

标签: php laravel testing

Laravel 5.7。我有这样的测试:

/** @test */
public function do_nothing_test()
{
    $user = factory(User::class)->states('app')->make();
    $this->actingAs($user, 'api');
    $response = $this->postJson('some_endpoint', ['foobar' => 3]);
    $this->assertEquals(1, Foobar::count());
}

在试图获取登录用户的控制器中调用端点:

public function update_foobar($request)
{
    $userId = auth()->user()->id;
    Foobar::create(['userId' => $userId, 'foobar' => $request->foobar]);
}

$id的值为null。如何使测试生效,以便控制器可以使用用户的详细信息?

1 个答案:

答案 0 :(得分:0)

// Setting up legend
var legend = new am4maps.Legend();
legend.parent = chart.chartContainer;
legend.background.fill = am4core.color("#000");
legend.background.fillOpacity = 0.05;
legend.y = am4core.percent(100);
legend.verticalCenter = "bottom";
legend.padding(10,15,10,15)
legend.data = [{
    "name": "Category #1",
    "fill": chart.colors.getIndex(0)
}, {
    "name": "Category #2",
    "fill": chart.colors.getIndex(1)
}, {
    "name": "Category #3",
    "fill": chart.colors.getIndex(2)
}, {
    "name": "Category #4",
    "fill": chart.colors.getIndex(2)
}];

// Does something when legend marker is clicked
legend.itemContainers.template.events.on("hit", function(ev) {
    var index = ev.target.dataItem.index;
    if (ev.target.isActive) { 
        //????.hide();
    }
    else {
        //????.show();
    }
})

$user = factory(User::class)->states('app')->make(); 不会保存到数据库,因此他们没有ID。我认为您是说make()