I'm trying to write a test class for a shopping cart. Here is what I 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() {
// just a placeholder at the moment
$this->assertTrue(true);
}
}
However, when I run phpunit, it seems like Laravel is unable to resolve my ShoppingCartClass.
Here is the error:
Fatal error: Uncaught exception 'Illuminate\Contracts\Container\BindingResolutionException'
with 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 my ShoppingCart class being resolved in a number of different controllers just fine.
Why can't Laravel resolve it during my tests?
I refered to this post as well but still didn't have any luck.
App\Classes\Billing\ShoppingCartplease?$this->app->make('App\Classes\Billing\ShoppingCart');as all Laravel tests have an instance of the application available to them as a property.$this->app->make('App\Classes\Billing\ShoppingCart')and am now getting a new error. Fatal error: Call to a member function make() on null__construct()to create instances of things. Try renaming__construct()tosetupShoppingCart()(or similar - the name doesn't matter) and create a docblock which has@beforein it, like this: /** * @before */