samedi 24 septembre 2016

Laravel Unit Test does not resolve class from Container

I have a Factory Class that has a static create function

<?php

    public static function create($type, $param, $countryCode = '')
    {
        // rest of code ... 
        return new Generator($strategy, app('myClass'));
    }
}

This function works fine when executed. But for the UnitTest, it returns error

ReflectionException: Class myClass does not exist

It is supposed to contact the Laravel IOC which actually returns the instance of MyClass like below

// AppServiceProvider

public function register()
{
    $this->app->bind('myClass', function ($app) {
        return new MyClass($app['option']);
    });
}

One more thing that my Test Classes are not extended with Laravel TestCase instead it is directly extended \PHPUnit_Framework_TestCase because this is a legacy code which cannot be changed atleast for now.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire