问题描述
没有通过最后一次测试,该测试将刚输入的字段与由于json发送的字段进行了比较
我有以下字段:
public function up()
{
Schema::create('polls',function (Blueprint $table) {
$table->id();
$table->text('Now');
$table->json('paramJson');
$table->enum('status',['a','b','c','d'])->default('a');
});
}
投票模型:
protected $fillable=[
'Now','paramJson',];
//to cast that column from JSON to an array automatically (maybe it doesn't work)
protected $casts = [
'paramJson' => 'array',];
PollsController.PHP
public function add(Request $request)
{
$poll = Poll::create($request->all());
return response()->json($poll,201);
}
Feature / PollApiTest.PHP
$data = [
"Now" => "Will Messi sign for City?","parameters" : {"a"=> "yes","b"=> "no"},];
$response = $this->post('/api/polls',$data);//add data in polls
$response ->assertStatus(201);// assert Ok
$response->assertJson($data);//assert Ok
$response = $this->get('/api/poll');//get index polls
$this->assertSame($dataDbTest[0]['paramJson'],$data['paramJson']);//this test fails
最后一次测试失败,因为在错误下方
无法断言数组&0( 'a'=>'是' 'b'=>'no')等同于'{“ a”:“ yes”,“ b”:“ no”}'。
如果我最后一次测试是用json_encode进行的:
$this->assertSame($dataDbTest[0]['paramJson'],json_encode($data['paramJson']));
--- Expected
+++ Actual @@ @@
-'{"a": "yes","b": "no"}'
+'{"a":"yes","b":"no"}'
解决方法
我认为应该是这样
$this->assertSame(json_decode($dataDbTest[0]['paramJson']),$data['paramJson']);