2

I'm trying to test if a "name" is actually inserted into my controller.

I created this test to check for the "name":

  public function testStoreName()
{
    $json = '{"name":"FOO", "address":"fubar City", "nickname":"fubar"}';
     $post = $this->action('POST', 'FooController@store', null, array(), array(), array(), $json);
     $output= json_decode($this->client->getResponse()->getContent());
     $output->name;



     if($output->name != "")
     {
         echo "Test Passed\n\n";
     }

     elseif($output->name == "")
     {
        echo "Name cannot be null";
     }

  //      $this->assertTrue($this->client->getResponse()->isOk());
}

But when I alter the $json and set "name": ""; I get errors. I want it to say that "name cannot be null.

1 Answer 1

2

Try this:

   public function testStoreName()
       {
            $json = '{"name":"FOO", "address":"fubar City", "nickname":"fubar"}';
            $jsonDecode = json_decode($json, true);
            $name = $jsonDecode['name'];
            $post = $this->action('POST', 'FooController@store', null, array(), array(), array(), $json);

            $this->assertTrue($this->client->getResponse()->isOk());

            $output= json_decode($this->client->getResponse()->getContent());

            $this->assertEquals($name, $output->name, 'Name incorrect'); 

        }

The code above decoes the json string and we put the 'name' element into $name and we do an assertEquals() to compare the json string 'name' that we specified towards the actual 'name' content that is being inputted into the controller.

Sign up to request clarification or add additional context in comments.

Comments

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.