使用工厂测试 Laravel post 请求
Testing Laravel post requests using a Factory
我正在为我的 Laravel 应用程序编写一些功能测试。我是 TDD 的新手,所以这对某些人来说似乎很明显。
LocationsFactory.php
use Faker\Generator as Faker;
$factory->define(App\Location::class, function (Faker $faker) {
return [
'name' => $faker->name,
];
});
LocationsTest.php
public function a_user_can_create_a_location(): void
{
$this->withExceptionHandling();
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', $location); // $location needs to be an array
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
}
TypeError: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::post() must be of the type array, object given
我知道错误告诉我 $location
需要是一个数组并且它是一个对象。但是,由于我使用的是工厂,所以它作为一个对象出现。在我的测试中有没有更好的方法来使用工厂?
这似乎也有点不对:
$this->assertDatabaseHas('locations', ['name' => $location->name]);
由于我使用的是 faker,所以我不知道 name
会是什么。所以我只是检查生成的内容是否理想?
感谢您的任何建议!
编辑
做这样的事情很好(也许这就是解决方案)...
...
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', [
'name' => $location->name
]);
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
但是,假设我的 location
有 30 个属性。这似乎很快就会变得丑陋。
Laravel 5
使用 toArray()
进行对象到数组的转换:参见以下示例
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', $location->toArray());
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
您也可以使用 raw
将属性构建为数组。
$user = factory(User::class)->make();
$location = factory(Location::class)->raw();
$response = $this->actingAs($user)->post('/locations', $location);
$response->assertStatus(200);
$this->assertDatabaseHas('locations', $location);
我正在为我的 Laravel 应用程序编写一些功能测试。我是 TDD 的新手,所以这对某些人来说似乎很明显。
LocationsFactory.php
use Faker\Generator as Faker;
$factory->define(App\Location::class, function (Faker $faker) {
return [
'name' => $faker->name,
];
});
LocationsTest.php
public function a_user_can_create_a_location(): void
{
$this->withExceptionHandling();
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', $location); // $location needs to be an array
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
}
TypeError: Argument 2 passed to Illuminate\Foundation\Testing\TestCase::post() must be of the type array, object given
我知道错误告诉我 $location
需要是一个数组并且它是一个对象。但是,由于我使用的是工厂,所以它作为一个对象出现。在我的测试中有没有更好的方法来使用工厂?
这似乎也有点不对:
$this->assertDatabaseHas('locations', ['name' => $location->name]);
由于我使用的是 faker,所以我不知道 name
会是什么。所以我只是检查生成的内容是否理想?
感谢您的任何建议!
编辑
做这样的事情很好(也许这就是解决方案)...
...
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', [
'name' => $location->name
]);
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
但是,假设我的 location
有 30 个属性。这似乎很快就会变得丑陋。
Laravel 5
使用 toArray()
进行对象到数组的转换:参见以下示例
$user = factory(User::class)->make();
$location = factory(Location::class)->make();
$response = $this->actingAs($user)->post('/locations', $location->toArray());
$response->assertStatus(200);
$this->assertDatabaseHas('locations', ['name' => $location->name]);
您也可以使用 raw
将属性构建为数组。
$user = factory(User::class)->make();
$location = factory(Location::class)->raw();
$response = $this->actingAs($user)->post('/locations', $location);
$response->assertStatus(200);
$this->assertDatabaseHas('locations', $location);