我知道我的问题标题可能不是最有用的信息,所以如果我能以某种方式改进它,请告诉我:)。
我试图弄清楚如何在 PHP 单元测试中传递 GraphQL 变量而不将它们内联写入查询中。
这是一个演示代码。我无法给出确切的真实源代码,因为它属于客户项目。我希望这个简化的版本能够显示问题。
class MyGraphQLTest extends Illuminate\Foundation\Testing\TestCase
{
use Tests\CreatesApplication;
use \Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
public function testSomething()
{
// Query an article with a specific id defined in this variable
$this->graphQL(/** @lang GraphQL */ '
{
article(id: $test_id) {
id,
name
}
}',
[
'test_id' => 5, // Does not work, the variable is not passed for some strange reason.
]
)->assertJson([
'data' => [
'article' => [ // We should receive an article with id 5 and title 'Test Article'
'id' => 5,
'name' => 'Test Article',
]
]
]);
}
}
根据这个Lighthouse: Testing with PHPUnit指南,变量应该能够作为数组作为->graphQL()
方法的第二个参数传递。
当我使用 运行测试时php vendor/bin/phpunit
,我收到以下错误响应:
[{
"errors": [
{
"message": "Variable \"$test_id\" is not defined.",
"extensions": {
"category": "graphql"
},
"locations": *removed as not needed in this question*
}
]
}].
灯塔是最新版本:4.15.0
谢谢您的支持!:)