спокойной ночи каждому. Мне нужна помощь, чтобы выбраться из того места, где я застрял ... так что я только начинаю работать с GraphQl / Rails и Rspe c .. и пытаюсь написать свой первый тест (rspe c -rails) для мутаций GraphQl ("graphql", "~> 1.9"), но я действительно не могу понять, чего не хватает в коде ...
discount_type.rb
module Types
class DiscountType < Types::BaseObject
field :client_id, Int, null: true
field :name, String, null: true
field :description, String, null: true
field :img, String, null: true
field :actual_price, Float, null: true
field :original_price, Float, null: true
end
end
client_type.rb
module Types
class ClientType < Types::BaseObject
field :name, String, null: false
field :information, String, null: false
end
end
client_factory.rb
FactoryBot.define do
factory :client do
sequence(:name) { "Client Name" }
sequence(:information) { "first client improved" }
end
end
discount_factory.rb
FactoryBot.define do
factory :discount do
client
sequence(:img) { "..." }
sequence(:name) { "first product" }
sequence(:description) { "my description" }
sequence(:client_id) { "1" }
sequence(:original_price) { "10" }
sequence(:actual_price) { "0.8" }
end
end
create_client.rb [мутация]
module Mutations
class CreateClient < BaseMutation
argument :name, String, required: true
argument :information, String, required: true
field :client, Types::ClientType, null: true
field :errors, [String], null: true
def resolve(name:, information:)
client = Client.new(name: name, information: information)
if discount.save
{ client: client, errors: [] }
else
{ client: nil, errors: client.errors.full_messages}
end
end
end
end
create_discount.rb [мутация]
module Mutations
class CreateDiscount < BaseMutation
argument :client_id, ID, required: true
argument :name, String, required: true
argument :description, String, required: true
argument :img, String, required: true
argument :original_price, Float, required: true
argument :actual_price, Float, required: true
field :discount, Types::DiscountType, null: true
field :errors, [String], null: true
def resolve(client_id:, name:, description:, img:, actual_price:, original_price:)
discount = Discount.new(
client_id: client_id, name: name, actual_price: actual_price,
original_price: original_price, img: img, description: description,
)
if discount.save
{ discount: discount, errors: [] }
else
{ discount: nil, errors: discount.errors.full_messages }
end
end
end
end
create_discount_spe c .rb
require 'rails_helper'
module Mutations
RSpec.describe Discount, type: :request do
describe '.resolve' do
it 'creates a discount' do
expect do
client = create(:client)
post '/graphql', params: {
query: <<~GQL
mutation { createDiscount(
input: {
clientId: '#{client.id}'
name: 'mais um produto'
description: '...'
originalPrice: '1'
actualPrice: '0.8'
img: '....'
}
) { clientId img name description originalPrice actualPrice }
}
}
GQL
}
end.to change { Discount.count }.by(1)
end
end
end
end
spe c output
Run options: include {:full_description=>/Discount/}
expected `Discount.count` to have changed by 1, but was changed by 0
0) Discount.resolve creates a discount
Failure/Error:
expect do
client = create(:client)
post '/graphql', params: {
query: <<~GQL
mutation { createDiscount(
input: {
clientId: '#{client.id}'
name: 'mais um produto'
description: '...'
originalPrice: '1'
expected `Discount.count` to have changed by 1, but was changed by 0
# ./spec/graphql/mutations/discounts/create_discount_spec.rb:8:in `block (3 levels) in <module:Mutations>'
1 example, 1 failure, 0 passed
Finished in 1.198049 seconds
Я сделал это publi c Gist и зовите на помощь .. Помогите! https://gist.github.com/eduardovrocha/09cf224fd08341c9d9f10c4169d5011a
Это публично c .. Так что каждый может улучшить. Я принимаю ссылки, учебные пособия .. видео на YouTube .. любая помощь приветствуется