Last active
December 4, 2020 17:50
-
-
Save aud/71233e4ff281c846ce6fbcf6350d8e42 to your computer and use it in GitHub Desktop.
`ruby -Itest example_test.rb`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
ruby '2.7.2' | |
gem 'rails', github: 'rails/rails', require: true | |
gem 'graphql', require: true | |
gem 'pry-byebug', require: true | |
gem 'mocha', require: true | |
gem 'minitest', require: true | |
end | |
class MyComplexLogicService | |
def self.do_stuff | |
{id: 1} | |
end | |
end | |
module Types | |
class Bar < GraphQL::Schema::Object | |
field :id, Integer, null: true | |
end | |
class Query < GraphQL::Schema::Object | |
field :bar, Bar, null: true | |
def bar | |
MyComplexLogicService.do_stuff | |
end | |
end | |
end | |
class ZoomSchema < GraphQL::Schema | |
query(Types::Query) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require_relative './example' | |
require 'minitest/autorun' | |
require 'mocha/minitest' | |
class ExampleTest < Minitest::Test | |
def test_my_complex_logic_service | |
result = MyComplexLogicService.do_stuff | |
expected_result = {id: 1} | |
assert_equal(expected_result, result) | |
end | |
def test_bar_query | |
query_string = <<~QUERY | |
query { | |
bar { | |
id | |
} | |
} | |
QUERY | |
MyComplexLogicService.expects(:do_stuff).returns({id: 1}) | |
result = ZoomSchema.execute(query_string).to_h | |
expected_result = { | |
"data" => { | |
"bar" => { | |
"id" => 1 | |
} | |
} | |
} | |
assert_nil(result['errors']) | |
assert_equal(expected_result, result) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment