2016-08-12 2 views
0

Laravel 5.1과 함께 제공되는 PHPUnit을 사용하고 있습니다. 내가 'UserTest.php'라는 이름으로 테스트 폴더 아래에 PHP 파일을 만들었습니다. 내가 명령 'phpunit을'을 실행 할 때마다laravel 5.1 - phpUnit 클래스를 문자열 예외로 변환 할 수 없습니다.

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')); 
    } 

: 나는 다음과 같은 코드를 서버 측에서

<?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, 
      ]);*/ 


    } 

을 다음과 같이 파일의 내용이다. UserTest 클래스의 객체를 string으로 변환 할 수 없다는 에러를 발생시킵니다. 내가 여기서 뭘 잘못하고 있는지 말해줘. $this 그래서 당신이 echo를 사용하여 문자열로 변환하려고

enter image description here

답변

1

$this->post()$this->get() 반환, 당신은 응답의 내용을 얻을 수 $this->response->getContent()을 사용해야합니다.

+0

고마워, 괜찮 았어. 내가 전화 할 때 seeJson도 사용하는 법을 말해 줄 수 있니? seeJson ([ 'success'=> true)]; 'foreach()'에 잘못된 인수가 제공되었습니다. @rypskar – logeeks

+0

seeJson을 사용하지 않아서 문제가 무엇인지 잘 모릅니다. json을 반환 할 때 나는 보통 $ response = json_decode ($ this-> response-> getContent());를 사용하고 다른 속성에 대해'assertEquals'를 사용하여 내가 올바른 데이터를 가지고 있는지 확인합니다 – rypskar

관련 문제