2016-10-26 6 views
1

phpunitのドキュメントに続いて、私は次のコードを思いついた。テストは失敗し、出力はスタブされたメソッドを呼び出すのではなく、実際のメソッドであり、データベースに当たってデータベースからデータを返すことを示しています。私は実際のクラスメソッドの代わりに呼び出されるようにテストダミーを "注入"するステップがないと思います。誰でも私がここで間違っていることを指摘できますか?Laravel 5.3でPHPUnitを使用したクラスメソッド呼び出しのスタブリング

私のテスト:

$shouldReturn = '[{"name":"A Category Name 1"},{"name":"A Category Name 2"},{"name":"A Category Name 3"}]'; 

// Create a mock for the CategoryClass, 
$catClassMock = $this->getMockBuilder(CategoryClass::class)->getMock(); 

// Set up the Test Dummy for the findAll method and stub what should be returned. 
$catClassMock->expects($this->once()) 
    ->method('findAll') 
    ->with($this->returnValue($shouldReturn)); 

// Setup the controller object, and call the index method. 
$CategoriesController = new CategoriesController(); 
$returnedResults = $CategoriesController->index(); 

// Assert the results equal what we told the method to return. 
$this->assertEquals($returnedResults, $shouldReturn); 

CategoriesController方法:

public function index() { 
    // List all category 
    return $this->categoryClass->findAll(); 
} 

注:ます$ this-> categoryClassはCategoriesControllerのコンストラクタメソッドでインスタンス化されています。 $ this-> categoryClass =新しいCategoryClass;

CategoryClassのfindAllメソッド:

public function findAll() { 
    // List all categories 
    $categories = Category::all(); // Eloquent call to database. 
    return json_encode($categories); 
} 

おかげ億!

+0

あなた自身が質問に答えてくれたと思います。「...私はテストダミーを注入するステップを逃しています...」 –

+0

それでは、私は正しい道を進んでいますか?ドキュメントに追加の手順はありませんが、これは私の問題の分析です。このスタブを「注入」する必要がある場合は、どうすればそれをやりますか?これは、私が使用しているドキュメントです:https://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs –

答えて

1

あなたが嘲笑クラスオブジェクトのメソッドをモックするとき、あなたはあなたのモックメソッドの応答を取得すること嘲笑クラスオブジェクトを使用する必要があります。

したがって、CategoriesController-> index()メソッドで実際のCategoriesクラスではなく模擬されたCategoriesクラスを呼び出す場合は、CategoriesクラスをCategoriesControllerクラスに注入する必要があります。このような何か作業をする必要があります:

$shouldReturn = '[{"name":"A Category Name 1"},{"name":"A Category Name 2"},{"name":"A Category Name 3"}]'; 

$catClassMock = $this->getMockBuilder(CategoryClass::class) 
    ->setMethods(['findAll']) 
    ->getMock(); 

// Set up the Test Dummy for the findAll method and stub what should be returned. 
$catClassMock 
    ->method('findAll') 
    ->willReturn($shouldReturn); 


// Setup the controller object, and call the index method. 
$CategoriesController = new CategoriesController(); 
$CategoriesController->categoryClass = $catClassMock; 
$returnedResults = $CategoriesController->index(); 

// Assert the results equal what we told the method to return. 
$this->assertEquals($returnedResults, $shouldReturn); 

私はまた、すべてのテストも動作し、あなたに雄弁クエリを模擬する必要性を奪うだろう前に、播種のインメモリのSQLiteデータベースを作成することを言及する必要があります。また、長期的に維持する方が簡単かもしれません。 LaravelのEloquentのテストデータベースを設定する方法の詳細については、https://laracasts.com/series/phpunit-testing-in-laravelをご覧ください。

0
クラス名にあなたの嘲笑インスタンスをバインド

$this->app->instance(CategoryClass::class, $catClassMock); 
+0

返事をありがとう、私はこのような何かを期待していた。残念ながら、それは私のために働いていません。私はまだ予想される$ shouldReturnの代わりに実際のデータベースデータを返します。 –

+0

代わりに更新された行を試すことはできますか?私はバインドが正しい方法ではなかったと思う。 –

+0

もう一度、本当にありがとう!それでも、正しく動作せず、データベース全体の出力が返されます。 –

関連する問題