【问题标题】:Laravel 5.5. Can't instantiate class with a specific name拉拉维尔 5.5。无法实例化具有特定名称的类
【发布时间】:2017-10-12 10:35:56
【问题描述】:

我有 2 个类:\App\Services\WeatherService\App\Services\WeatherService1 两者都是相等的。

在我的\Tests\Unit\Services\WeatherServiceTest 中,我正在尝试创建一个新实例,但我无法实例化\App\Services\WeatherService

//Works perfect
$some = new WeatherService1(new \Forecast\Forecast(config('darksky.key')));

dd($some);

/*
App\Services\WeatherService1 {#2405
  -forecastApi: Forecast\Forecast {#2406
    -api_key: "somekey"
  }
}
*/

原始气象服务提供

//Not working
$some = new WeatherService(new \Forecast\Forecast(config('darksky.key')));

dd($some); //app\Services\WeatherService {#2405}

可以做些什么来解决这个问题?

【问题讨论】:

  • 你的日志文件里有什么?看起来您没有将预测存储在 WeatherService 类的属性中。需要看课app\Services\WeatherService BTW 试试我们App\Services\WeatherService
  • @Webdesigner 问题是我已经从WeatherService1 复制粘贴WeatherService 并且只重命名了它。其余部分相同。
  • 但是有些东西肯定不一样,也许你错过了什么
  • @Webdesigner 不。我已经转移到 Docker,现在它可以正常工作,无需任何更改。

标签: php laravel laravel-5 phpunit laravel-5.5


【解决方案1】:

在另一个测试中,我有这样一个use 声明。

use app\Services\WeatherService;

改成

use App\Services\WeatherService;

已经解决了问题。

这是类的代码。可能,某人知道为什么会这样。但这很奇怪。

class WeatherControllerTest extends TestCase
{
    use WithoutMiddleware;

    private $mock;
    private $cities;

    protected function setUp()
    {
        parent::setUp();

        $this->mock = \Mockery::mock(WeatherService::class);
    }


    public function tearDown()
    {
        \Mockery::close();
    }

    //...
}    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-26
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多