0

I have following test function to check the update data is correct or not. It has no problem in updating. My question is how to check the given parameters are correct after updated.

for example

if response.id == 1 and response.name = 'Mr.Smith'
    assertcode = OK
else 
    assertcode = NG
public function user_update_info(){
        $this->post('login',['email' => config('my-app.test_user'),
            'password' => config('my-app.test_pass')]);
        $response = $this->post('/update_info',[
            'id'                => 1,
            'name'              => 'Mr.Smith',
            'post_code'         => '142-4756',
            'prefectural_code'  => '15',
            'address'           => 'Merchat St.',]);
        $response->assertStatus(200);
    }
1
  • Did you mean that you want to confirm what is in the json response or you want to check if the data is saved correctly into the database? Commented Oct 28, 2019 at 8:28

1 Answer 1

2

Assume your update_info route update User model.

Try below after your code,

$user = User::find(1);
static::assertTrue($user->id == 1 && $user->name = 'Mr.Smith');

To check if the response returns a json data you expect, you can use assertJson() method of the response object like so:

$response->assertJson([
    'id' => 1,
    'name' => 'Mr.Smith'
]);
Sign up to request clarification or add additional context in comments.

6 Comments

This answer doesn't say how to tests json response. Or am I missing something?
@OluwatobiSamuelOmisakin I agree with you. Since the question is asking for assertJson, I think it's not answering his question.
My answer is not very accurate. I have the same question as @Oluwatobi Samuel Omisakin. It can check if the data is saved correctly into the database. If want assertJson please see laravel.com/docs/6.0/http-tests#testing-json-apis
I think the OP's question also is ambiguous in the sense that it appears as though it is requesting for assertJson or assertDatabaseHas. I could see that this answer is already accepted, therefore it seems as though the OP wants to see if the data is persisted in the database, therefore makes this answer correct. I will make an update to the answer if you don't mind @liquid207
@OluwatobiSamuelOmisakin That's what i've thinking too. Since the OP has already accepted the answer, I can't fully blame @liquid207 for his answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.