简体   繁体   English

使用 Rspec 在 Rails 上测试 graphQL 突变,resolve 返回错误数量的参数

[英]testing graphQL mutations on rails using Rspec, resolve returns wrong number of arguments

I'm trying to test graphQL mutations but I couldn't solve the error.我正在尝试测试 graphQL 突变,但我无法解决错误。 I want to create a book for each author我想为每个作者创作一本书

# book_type.rb
module ApiTypes
  class BookType < BaseObject
    field :id, ID, null: false
    field :author, ::ApiTypes::UserType, null: false

    def author
      User.find(current_user.id)
    end
  end
end
# mutation_type.rb
module ApiTypes
  class MutationType < ::ApiTypes::BaseObject
    field :create, mutation: ::Mutations::Book::Create
  end
end
# book/creat.rb
module Mutations
  module Book
    class Create < ::Mutations::BaseMutation

      field :create_book, ::ApiTypes::BookType, null: false
      argument :genre_id, ID, required: true

      def resolve(gener_id:)
        create_book = Book.create_book(genre_id: genre_id, author_id: current_user.id)
      end

      def ready?(**_args)
        require_current_user!
      end
    end
  end
end

create_book def in book.rb在 book.rb 中创建_book def

def self.create_book(genre_id, author_id)
    # .. some checks...
    create(
      genre_id: genre_id,
      author_id: author_id
    )
  end

the test:考试:

# book/create_spec.rb
require "rails_helper"

RSpec.describe “createBookForAuthor mutation" do
  it "creates new book” do
    user = FactoryBot.create(:user)
    genre = FactoryBot.create(:genre)

    query_string = <<-GRAPHQL
    mutation {
      createNewBook(input: { genreId: #{genre.id} }) {
        createBook {
          id
        }
      }
    }
    GRAPHQL

    result = LibrarySchema.execute(query_string, context: { current_user: user })

    expect { result }.to change { Book.count }.by(1)

  end
end

the error I get when I run the test:我运行测试时遇到的错误:

Failure/Error: def self.create_book(genre_id, author_id) create( genre_id: genre_id, author_id: author_id ) error ArgumentError: wrong number of arguments (given 1, expected 2)失败/错误:def self.create_book(genre_id,author_id)create(genre_id:genre_id,author_id:author_id)错误ArgumentError:错误数量的参数(给定1,预期2)

Here you use hash as single argument when invoke method:在这里,您在调用方法时使用哈希作为单个参数:

create_book = Book.create_book(genre_id: genre_id, author_id: current_user.id)
# it's like Book.create_book({ genre_id: genre_id, author_id: current_user.id })

But your method uses two parameters:但是您的方法使用两个参数:

def self.create_book(genre_id, author_id)
  # method body
end

So you need choose one of these ways and use it when define method and when call it因此,您需要选择其中一种方式并在定义方法和调用它时使用它

Using hash is more flexible使用哈希更灵活

So you can define method as所以你可以将方法定义为

def self.create_book(args)
  # use args[:genre_id] and args[:author_id] inside
end

Or may be keyword arguments或者可能是关键字参数

def self.create_book(genre_id:, author_id:)
  # use genre_id and author_id inside
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM