2016-05-25 9 views
1

私は新しいカスタム例外をスローし、実際にスローされるようにテストを書いています。私はAgreementsNotSignedException.phpPHPUnitがLaravelでカスタム例外を見つけられません5.2

<?php 

namespace App\Exceptions; 

class AgreementsNotSignedException extends \Exception {} 

\次の場所のApp \例外で新しい例外を作成しましたし、私のための「チェックアウト」メソッドは次のようになります。ルックスを失敗している

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use App\Exceptions\AgreementsNotSignedException as AgreementsNotSignedException; 

class Order extends Model 
{ 
    public function checkout() 
    { 
     throw new AgreementsNotSignedException("User must agree to all agreements prior to checkout."); 
    } 
} 

私の基本的なテストこのように:

<?php 

use App\Order; 
use App\Exceptions\AgreementsNotSignedException; 

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

class OrderTest extends TestCase { 
    /** @test * */ 
    function it_does_not_allow_the_user_to_checkout_with_unsigned_contracts() 
    { 
     $exceptionClass = get_class(new App\Exceptions\AgreementsNotSignedException()); 
     $this->setExpectedException(
      $exceptionClass, 'User must agree to all agreements prior to checkout.' 
     ); 

     try { 
      $this->order->checkout(); 
     } catch (App\Exceptions\AgreementsNotSignedException $exception) { 
      $this->assertNotEquals($this->order->status, "completed"); 
     } 
    } 
} 

吐き出すメッセージです「タイプ 『AgreementsNotSignedException \アプリケーション\例外』の例外を主張する失敗がスローされます。」。しかし、xdebugを使って例外が実際にキャッチされたことを確認できます。ジェフはコメントで述べたように、それは、これはテストパスを作ることなど、FQNの問題のように見える:

/** @test * */ 
function it_does_not_allow_the_user_to_checkout_with_unsigned_contracts() 
{ 
    $exceptionClass = get_class(new App\Exceptions\AgreementsNotSignedException()); 
    $this->setExpectedException(
      $exceptionClass, 'User must agree to all agreements prior to checkout.' 
     ); 

    throw new App\Exceptions\AgreementsNotSignedException('User must agree to all agreements prior to checkout.'); 
} 

私はFQNを変更することで、チャンスをうかがっておこうが、任意のガイダンスは素晴らしいだろう。

+0

[この質問](http://stackoverflow.com/q/14572469/697370)のように、予期される例外の完全修飾名前空間を指定する必要があります –

+0

OrderTestで$ this-> orderがどのように定義されていますかクラス? – Matteo

+0

設定方法の中で割り当てられた保護された変数です(写真は表示されていません)。 order変数は問題なく、checkoutを呼び出す前にデバッガで状態を確認できます。 – Msencenb

答えて

0

で試してみてください。これに代えて

$this->setExpectedException(
      'App\Exceptions\AgreementsNotSignedException, 'User must agree to all agreements prior to checkout.' 
     ); 

$exceptionClass = get_class(new App\Exceptions\AgreementsNotSignedException()); 
     $this->setExpectedException(
      $exceptionClass, 'User must agree to all agreements prior to checkout.' 
     ); 
+0

残念ながら、同じエラーです。私はこれを最初に試して、get_classメソッドに落として、より良いものを修飾することを期待しました。私はassertThatとPHPUnit_Framework_Constraint_Exceptionを使ってこのテストを動作させました。 – Msencenb

1

ただPHPUnitの$this->setExpectedException()法は廃止され、同じ問題を抱えていたし、ソースコードに基づいて、解決策を見つけました。

* * @since Method available since Release 3.2.0 * @deprecated Method deprecated since Release 5.2.0 */ public function setExpectedException($exception, $message = '', $code = null) {

私の代わりに$this->expectException(MyCustomException::class)を使用し、それが働きました。

関連する問題