【问题标题】:i'm doing test in laravel whene run command php artisan test see this error我在 laravel 中做测试时运行命令 php artisan test 看到这个错误
【发布时间】:2021-10-23 02:33:26
【问题描述】:

PS C:\xampp\htdocs\laravel_testing> PHP 工匠测试 警告:Windows 平台不支持 TTY 模式。

运行测试\Unit\AccountantHelperTest • 它可以找到利润

测试:3 个待定

失败测试\Unit\AccountantHelperTest ✕它可以找到利润

测试:1 个失败,2 个待处理

错误

找不到类“App\AccountantHelper”

在 C:\xampp\htdocs\laravel_testing\tests\Unit\AccountantHelperTest.php:16 12| * @测试 13| */ 14|公共函数 it_can_find_profit() 15| {

16| $利润= AccountantHelper::findProfit(100); 17| $this->assertEquals(10,$profit); 18| 19| } 20| }

1 C:\xampp\htdocs\laravel_testing\vendor\phpunit\phpunit\src\Framework\TestCase.php:1472 测试\单位\AccountantHelperTest::it_can_find_profit()

2 C:\xampp\htdocs\laravel_testing\vendor\phpunit\phpunit\src\Framework\TestCase.php:1092 PHPUnit\Framework\TestCase::runTest()

AccountantHelper 是文件夹 app 中的一个类

<?php
namespace App;


class AccountantHelper
{

function Profit($amount)
{

    $profitPercent =10;
    return $profitPercent * $amount / 100 ;
}

}

AccountantHelperTest 是测试/单元中的文件

<?php

namespace Tests\Unit;
use App\AccountantHelper;
use PHPUnit\Framework\TestCase;

class AccountantHelperTest extends TestCase
{
    /** @test  */
  function it_can_find_profit()
  {
          $Profit= AccountantHelper::Profit(100);
          $this->assertEquals(10,$Profit);

  }
}

【问题讨论】:

  • 将代码添加到您的问题中。 AccountantHelperTest.php 和 AccountantHelper.php
  • "找不到类 'App\AccountantHelper'" 你在测试中 use 了吗?
  • 确定我会添加有问题的代码
  • Profit 函数不是静态的,因此您需要创建一个 new AccountantHelper 才能使用它(或使其成为静态)。 static keyword in the manual
  • 我确实这样做了,但出现了同样的问题

标签: php laravel unit-testing testing laravel-testing


【解决方案1】:

AccountantHelper 类中的 Profit 方法不是静态的。你的代码应该是这样的:

class AccountantHelper
{
    public static function Profit($amount)
    {
        $profitPercent =10;
        return $profitPercent * $amount / 100 ;
    }
}

另外,如果那是一个 Eloquent 模型,并且您在那里有很多查询,您可以使用 scope

【讨论】:

  • 我确实这样做了,但出现了同样的问题
  • 为什么找不到“App\AccountantHelper”类??
  • 您确定AccountatnHelper 路径吗?是在app/ 目录中吗?
猜你喜欢
  • 2019-09-13
  • 2022-01-05
  • 2019-02-03
  • 2016-03-24
  • 2012-06-16
  • 2019-02-17
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
相关资源
最近更新 更多