0

I have some test for field validation. How I can assert response error message? this is my simple test

/** @test */
public function categoryRequiresName()
{



$response = $this->postGraphQL([
    'query' => '
        mutation CreateCategory($name: String!) {
            createCategory(name: $name) {
                name
            }
        }
    ',
    'variables' => [
        'name' => '',
    ],
]);

// how to assert Response message ?
// edited solution
$this->assertEquals("The name field is required.", $response->json("errors.0.extensions.validation.name.0"));
}

this is my rule at mutation field

name: String @rules(apply: ["required"])

this is error tree

enter image description here

  • Are you trying to validate, if you are getting correct response message? if yes check https://github.com/PrafullaKumarSahu/unittest/blob/master/tests/Feature/CarouselFeatureTest.php – Prafulla Kumar Sahu Sep 14 '19 at 09:08
  • as you can see on screenshot i debugged response. it gives correct message but i don't know how assert it –  Sep 14 '19 at 09:14
  • If you want to assert the whole `$errors` array that will be difficult, if you want to assert the message, you can just compare the message, for `$errors` array, you may need to write a customize function to check whatever in that array and assert the return boolean value here. – Prafulla Kumar Sahu Sep 14 '19 at 09:22
  • ok thx bro... i need just test message which contains name array e.g "The name field is required" –  Sep 14 '19 at 09:35
  • Then use `assertSessionHas('message', 'your message to compare')`. – Prafulla Kumar Sahu Sep 14 '19 at 09:36
  • @PrafullaKumarSahu thank u. i passed test with `$this->assertEquals("The name field is required.", $response->json("errors.0.extensions.validation.name.0"));` –  Sep 14 '19 at 09:59
  • `assertSessionHas` not working? – Prafulla Kumar Sahu Sep 14 '19 at 10:25
  • 1
    nope :( ....... –  Sep 14 '19 at 10:34

0 Answers0