Ruby on rails 如果设置了graphQL字段方法参数默认值,则使用RSpec进行测试
如何编写测试来检查field methods参数的默认值Ruby on rails 如果设置了graphQL字段方法参数默认值,则使用RSpec进行测试,ruby-on-rails,ruby,rspec,graphql,Ruby On Rails,Ruby,Rspec,Graphql,如何编写测试来检查field methods参数的默认值 字段:foo,字符串,null:false do 参数my_参数Int,必选:true 参数my_other_参数,布尔型,必填项:false 结束 def foo(我的参数:,我的其他参数:true) 结束 我在RSpec测试中的尝试: 字段:foo,“String!”do 它“测试my_other_参数的默认值是否为true” resolve(args:{my_argument:10})#注意,my_other_argument没有
字段:foo,字符串,null:false do
参数my_参数Int,必选:true
参数my_other_参数,布尔型,必填项:false
结束
def foo(我的参数:,我的其他参数:true)
结束
我在RSpec测试中的尝试:
字段:foo,“String!”do
它“测试my_other_参数的默认值是否为true”
resolve(args:{my_argument:10})#注意,my_other_argument没有给定值
expect(args[:my_other_参数])到eq(true)
结束
结束
上述示例失败并引发此错误:
undefined local variable or method `args'
总而言之,我似乎不知道这一行应该怎么写:
expect(args[:my_other_argument]).to eq(true)
。。。或者我完全走错了路?如果
foo
正在调用另一个方法,您可以使用receive
和with
。类似于
class Foo
def bar(my_other_argument: true)
baz(my_other_argument)
end
def baz(val)
# Stuff
end
end
describe :bar do
context 'default values' do
it 'defaults to true' do
@foo = Foo.new
expect(@foo).to receive(:baz).with(true)
@foo.bar
end
end
end
让我们考虑一下这个问题。
您可以为foo
定义解析程序
class Resolvers::Foo < Resolvers::Base
argument :my_argument, Int, required: true
argument :my_other_argument, Boolean, required: false
def resolve(my_argument:, my_other_argument: true)
<some code>
end
end
另一方面,如果要测试带有参数的某些字段,最好使用以下参数进行查询:
foo(my_argument: $my_argument, my_other_argument: $my_other_argument)
$my_argument
和$my_other_argument
是您要测试的参数。
在的文档中有更多详细信息,但看起来并不是问题的答案
foo(my_argument: $my_argument, my_other_argument: $my_other_argument)