php - Laravel Unit Testing Dependency Injection -
i'm trying write test class shopping cart. here have:
shoppingcarttest.php
class shoppingcarttest extends testcase { use databasetransactions; protected $shoppingcart; public function __construct() { $this->shoppingcart = resolve('app\classes\billing\shoppingcart'); } /** @test */ public function a_product_can_be_added_to_and_retrieved_from_the_shopping_cart() { // placeholder @ moment $this->asserttrue(true); } }
however, when run phpunit, seems laravel unable resolve shoppingcartclass.
here error:
fatal error: uncaught exception 'illuminate\contracts\container\bindingresolutionexception' message 'unresolvable dependency resolving [parameter #0 [ <required> $app ]] in class illuminate\support\manager' in c:\development server\easyphp-devserver-16.1\eds-www\nrponline\vendor\laravel\framework\src\illuminate\container\container.php:850
i have shoppingcart class being resolved in number of different controllers fine.
why can't laravel resolve during tests?
i refered this post still didn't have luck.
i figured out. here updated class.
class shoppingcarttest extends testcase { use databasetransactions; protected $shoppingcart; public function setup() { parent::setup(); $this->shoppingcart = $this->app->make('app\classes\billing\shoppingcart'); } /** @test */ public function a_product_can_be_added_to_and_retrieved_from_the_shopping_cart() { // placeholder @ moment $this->asserttrue(true); } }
thanks @edcs guiding me in right direction. need use setup function , not __construct
app
instance hasn't been created yet.
Comments
Post a Comment