Ruby on Rails Integration Testing with Minitest and Capybara

Ruby on Rails Integration Testing with Minitest and Capybara

Ruby on Rails

Hi Guys! I am back again with one more article about ruby on rails.

I hope this is the right time to share one of my technical stuffs on ruby on rails after sharing PHP and Mobile stuffs.

Minitest and Capybara: integration testing demystified

In this article, I am going to share my own experience and ideas of ruby on rails integration testing using Minitest and Capybara and tell you the process how it serves as one of the most preferred alternative for controller test.

A lot of you must have heard about Capybara but still for those who are new to this –

Capybara is an acceptance test framework used for web applications and supports developers in integration testing of Ruby on Rails applications with Minitest.

So, let’s get into the details about Capybara, Minitest and Integration Testing

Minitest is the default and a complete testing suite for Ruby application, which supports ancillary test-driven development (TDD), behavior-driven development (BDD), mocking, and benchmarking for fast, clean and reliable test results.

Whereas, Capybara is the acceptance test framework for web applications and frequently used for end-to-end testing in Rails Applications.

Rails developer can suggest user on web page and Capybara provides API to interact with web page.

Integration Testing are used to test the most important workflows of applications and test different fragments of application together while unit testing inspects the individual part of the application work.

Before sharing the real process how to use Capybara with Minitest for integration testing of your Ruby on Rails applications, let me tell you the prerequisites of this testing process.

Ruby version 2.3.3, Rails version 5.0.0.1,
Minitest version 5.10.1, and Capybara version 2.11.1 are required for the smooth operation and you can use gem install rails to get started.

Setup

You can set up a new Rails application.

rails new integration-testing-minitest

We’ll need to add Capybara to our – Gemfile

in the group test.

[code language=”php”]# Gemfile

group :test do
gem ‘minitest-rails-capybara’
end
[/code]

Also need to load Capybara in order to use it in our tests.

[code language=”php”]
# test/test_helper.rb

ENV[‘RAILS_ENV’] ||= ‘test’
requireFile.expand_path(‘../../config/environment’, __FILE__)
require ‘rails/test_help’
require "minitest/rails/capybara"

[/code]

Now, that you have everything set up, let’s see the integration tests with an example application.

Running the scaffold generator to create posts.

[code language=”php”]
rails generate scaffold Post title:string body:text
[/code]

Next we need to migrate the database.

[code language=”php”]
rake db:migrate
== 20161214213527 CreatePosts: migrating ======================================
— create_table(:posts)
-> 0.0020s
== 20161214213527 CreatePosts: migrated (0.0024s) =============================
rake
# Running:
…….
[/code]

You can also issue

[code language=”php”]rails server[/code]

at the command line and navigate to http://localhost:3000/posts to check the result.

Integration Tests

 Let’s create our first integration test.

[code language=”php”]
# test/fixtures/posts.yml

one:
title: Post Title One
body: Post body one.

two:
title: Post Title Two
body: Post body two.
[/code]

Here’s our first integration test.

[code language=”php”]
# test/integration/post_flow_test.rb

require ‘test_helper’

classPostFlowTest> Capybara::Rails::TestCase
def setup
@one = posts :one
@two = posts :two
end

test ‘post index’ do
visitposts_path

assertpage.has_content?(@one.title)
assertpage.has_content?(@two.title)
end
end
[/code]

All you need to do here is – to run all your tests again to make sure they all pass

[code language=”php”]
rake
# Running:
……..
Finished in 0.515325s, 15.5242 runs/s, 21.3458 assertions/s.

8 runs, 11 assertions, 0 failures, 0 errors, 0 skips
[/code]

Now, let’s move on to something a bit more complicated, and test if you can write a new post and submit it. Place it below other test in your integration test for posts.

[code language=”php”]
# test/integration/post_flow_test.rb

test ‘writing a new post’ do
visitposts_path

click_on ‘New Post’

fill_in ‘Title’, with: ‘Test Title’
fill_in ‘Body’,  with: ‘Test Body’

click_on ‘Create Post’

assert_current_pathpost_path(Post.last)
assertpage.has_content?(‘Test Title’)
assertpage.has_content?(‘Test Body’)
end
end
[/code]

Run the tests again to make sure everything passes.

[code language=”php”]
rake

# Running:

………

Finished in 0.551475s, 16.3199 runs/s, 23.5731 assertions/s.

9 runs, 13 assertions, 0 failures, 0 errors, 0 skips
[/code]

Now, we have one last feature to add, the email alert to an admin email once a post has been submitted. Let’s start by adding a new test for writing a new post and checking if an admin notice email was sent.

[code language=”php”]
# test/integration/post_flow_test.rb

require ‘test_helper’

classPostFlowTest> Capybara::Rails::TestCase
includeActiveJob::TestHelper

def setup
@one = posts :one
@two = posts :two
end

test ‘post index’ do
visitposts_path

assertpage.has_content?(@one.title)
assertpage.has_content?(@two.title)
end

test ‘writing a new post’ do
write_new_post

latest_post = Post.last

assert_current_pathpost_path(latest_post)
assertpage.has_content?(‘Test Title’)
assertpage.has_content?(‘Test Body’)
end

test ‘writing a new post’ do
write_new_post

latest_post = Post.last

assert_current_pathpost_path(latest_post)
assertpage.has_content?(‘Test Title’)
assertpage.has_content?(‘Test Body’)
end

test ‘writing a new post sends admin notice’ do
perform_enqueued_jobs do
write_new_post

last_post = Post.last
mail      = ActionMailer::Base.deliveries.last

assert_equal ‘admin@example.com’, mail[‘to’].to_s
assert_equal ‘New post added’, mail.subject
end
end

private

defwrite_new_post
visitposts_path

click_on ‘New Post’

fill_in ‘Title’, with: ‘Test Title’
fill_in ‘Body’,  with: ‘Test Body’

click_on ‘Create Post’
end
end
[/code]

Let’s create the mailer first:

[code language=”php”]
rails generate mailer PostMailer
[/code]

This should set up the mailer – Time to add our admin notice email to it:

[code language=”php”]
# app/mailers/post_mailer.rb

classPostMailer> ApplicationMailer
defadmin_notice(post)
@post = post
mail to: ‘admin@example.com’, subject: ‘New post added’
end
end
[/code]

We also need the corresponding views:

[code language=”php”]
%# app/views/post_mailer/admin_notice.html.erb %>

A new post has been added! Here’s the post:

<%= @post.title %>
<%= simple_format @post.body %>

<%# app/views/post_mailer/admin_notice.text.erb %>

A new post has been added! Here’s the post:

Title: <%= @post.title %>
<%= @post.body %>
[/code]

We’ll skip the tests for this mailer to keep this tutorial from getting too long. All you have to do now is call the mailer from the controller after a post has been created.

[code language=”php”]
# app/controllers/posts_controller.rb


# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
PostMailer.admin_notice(@post).deliver_later

format.html { redirect_to @post, notice: ‘Post was successfully created.’ }
format.json{ render :show, status: :created, location: @post }
else
format.html { render :new }
format.json{ renderjson: @post.errors, status: :unprocessable_entity }
end
end
end

[/code]

We added only one line there to call the mailer. Now, let’s run the tests again and see if they pass.

[code language=”php”]
rake

# Running:

……….

Finished in 0.975611s, 10.2500 runs/s, 15.3750 assertions/s.

10 runs, 15 assertions, 0 failures, 0 errors, 0 skips
[/code]

All the tests should pass, and now you have an application that is integration-tested end-to-end with Minitest and Capybara.

Coming to an end of the lesson

I am sure this article will give you a clear idea about ruby on rails application integration testing using Minitest and Capybara. Share your thoughts with comments if I have missed anything or if you want to know more.

Jay
Jayadev Das
jayadev.das@andolasoft.com

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!