0

I am dabbling with PHPUnit that comes with the Laravel 5.1. I have create a php file under the tests folder with the name 'UserTest.php'. The content of the file is as follows

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class UserTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */



        public function testRegistration()
    {
         echo $this->post('/user/register', ['password' => 'mypass',
                                        'name' => 'Steve Robinson',
                                        'email' => '[email protected]']);
             /*->seeJson([
                 'success' => true,
             ]);*/


    }

In the server side I have the following code:

    public function store()
    {
        $credentials = Input::only('name','email', 'password');

   try {
       $user = User::create($credentials);
   } catch (Exception $e) {
       return Response::json(['error' => 'User already exists.'], HttpResponse::HTTP_CONFLICT);
   }

   $token = JWTAuth::fromUser($user);

   return Response::json(compact('token'));
    }

Whenever I run the command 'phpunit' . It throws an error saying object of class UserTest cannot be converted into string. Please tell me what am I doing wrong here.

enter image description here

1 Answer 1

1

$this->post() and $this->get() returns $this so you are trying to convert it to a string by using echo, you have to use $this->response->getContent() to get the content of the response.

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

2 Comments

Thanks, that worked fine .Can you please tell me how to use seeJson as well when I call ->seeJson(['success' => true, ]); it is saying 'invalid argument supplied for ' foreach()'' . @rypskar
I haven't used seeJson, so not sure what the problem might be. When returning json I normally use $response = json_decode($this->response->getContent()); and use assertEquals on the different properties to verify that I got the right data

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.