Testing GraphQL Mutations In Ruby On Rails With RSpec

Testing GraphQL Mutations In Ruby On Rails With RSpec

・3 min read
Testing GraphQL Mutations In Ruby On Rails With RSpec

You may also like:

Design SaaS Product: 4 Reasons Why You Should Choose Ruby On Rails Framework

Read more

In the last article we added a few GraphQL mutations to our test application. It is time to create automated tests for each of them. But first things first, we need to set up RSpec in order to write better specs.

RSpec and FactoryBot

Installing Rspec and Factory Bot are trivial. First, you have to add those gems to your Gemfile:

run bundle install

and then run rails generate rspec:install

The last thing I recommend to do is adding this line to spec/rails_helper.rb

Thanks to that line we will be able to use shortened syntax for building and creating objects using factories. It means that instead of FactoryBot.create(:book) we will be able to simply write create(:book).


I mentioned that we will be able to create objects easily thanks to FactoryBot. But in order to have this possibility, we have to define those factories first. We need to create two of them:



Now we have everything in place and we are able to start writing specs. But please note that for now, we will cover only the happy paths.

CreateBook spec

During the creation of the object, the most important assertion we can write is checking if the object is created and its fields in the database are correct. But when we are testing GraphQL endpoint we also want to make sure that it returns the proper object. Knowing all that, let’s take a look at the final version of our test. Following the RSpec convention, we are going to save our file in specs folder, in the same tree directory as the original file. In short, we save our spec in specs/graphql/mutations/books/create_book_spec.rb

As you can see, we test .resolve method and we check the things that I mentioned earlier, so writing and returning the object. There are two separates specs and we want to use the same query for both of them. Because of that, we define the query function when we pass the arguments that matter for us. In this case, we only need to pass author_id.

UpdateBook spec

In updateBook spec we are going to test if an object is changed after the operation and if the correct object is returned. The code will be really similar to the previous one and we place it in specs/graphql/mutations/books/update_book_spec.rb

DestroyBook spec

The last spec will be written for destroyBook mutation. We will focus on checking if the object is removed and if we return removed object after the action is finished. As you probably guessed, we save the code in specs/graphql/mutations/books/update_book_spec.rb


Writing the specs for GraphQL mutations isn’t that different from writing specs for the controller’s actions. The only difference might be defining the query method. I highly encourage you to use <<\~ to write a multiline query and increase the readability. Other than that, you simply have to focus on things and edge cases you want to cover in your specs. That’s all. Happy coding!

Rate this article:


based on 0 votes
Our services
See what we can create for You
Our services

Awards & Certificates

reviewed on
30 reviews
  • Top 1000 Companies Global 2021
  • Top Development Company Poland 2021
HR dream team
  • 2020 HR Dream Team Award
  • 2016 Employer Branding Featured
  • 2015 HR Dream Team Award
ISO CertificateISO CertificateAWS Certificate
  • Information Security Management System compliant with PN-EN ISO/IEC 27001
  • Business Continuity Management compliant with ISO 22301
  • AWS Certified Solutions Architect - Associate