Skip to content

Instantly share code, notes, and snippets.

@gringocl
Last active August 29, 2015 14:01
Show Gist options
  • Save gringocl/4381fcdfe7989a386be4 to your computer and use it in GitHub Desktop.
Save gringocl/4381fcdfe7989a386be4 to your computer and use it in GitHub Desktop.
Setting up a new rails project with minitest and capybara

#Setup new rails project with minitest and capybara for Behaviour Driven Development Introduction to Minitest.... We'll be setting up a new rails app with minitest and capybara so we can go over the steps to getting this going. We'll be using a rails 4.1+ in this example. Lets get started!

First thing create a new rails application, a git repository, and the first commit to the repo.

rails new awesomeapp
cd awesomeapp
git init
git add -A
git commit -m "Initial Commit"

Next open the Gemfile and add the minitest-rails and minitest-rails-capybara gems. Note the minitest-rails-capybara needs to grouped under test, while minitest-rails does not.

gem "minitest-rails"

group :test do
  gem "minitest-rails-capybara"
end

Next bundle your Gemfile and run the minitest-rails installation generator. This will overwrite the test_helper.rb that is in the test directory. Press y when prompted to overwrite this file.

bundle install
rails generate minitest:install

Open up test_helper.rb and uncomment require "mintitest/rails/capybara" and optionally minitest/pride. The first require statement adds capybara for our feature tests and the second adds awesome colors to the test suite, colors are always a bonus! Also you can remove the comments and as they are no longer needed and your test_helper.rb should look similar to the one below.

ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
require "minitest/rails"
require "minitest/rails/capybara"
require "minitest/pride"

class ActiveSupport::TestCase
  ActiveRecord::Migration.check_pending!
  fixtures :all
end

Now there are a few ways to generate tests and you can choose either TestCase or Spec DSL to write the tests. This article by Matt Sears is a great quick reference to both of these DSLs. While it is possible to remember to put the --spec flag on the generator commands, it is also possible to set these defaults in the config/application.rb.

config.generators do |g|
  g.test_framework :minitest, spec: true, fixture: true
end

Now add a features rake task, since we'll be writing feature specs and also add this as the default test task to run. Create a new file named features.rake in the lib/tasks/ folder and add the following.

Rails::TestTask.new("test:features" => "test:prepare") do |t|
  t.pattern = "test/features/**/*_test.rb"
end

Rake::Task["test:run"].enhance ["test:features"]

Now that Minitest and Capybara our setup, lets commit the changes to git and then create a new feature spec.

git add -A
git commit -m "Add Minitest-Rails and Minitest-Rails-Capybara gems and configure for Spec DSL"
rails generate minitest:feature HelloWorldDisplayedOnHomePage

Inspecting the test/features directory, a new file has been created hello_world_displayed_on_home_page_test.rb Open this and take a look at the generated boilerplate spec that has been created.

require "test_helper"

feature "HelloWorldDisplayedOnHomePage" do
  scenario "the test is sound" do
    visit root_path
    page.must_have_content "Hello World"
    page.wont_have_content "Goobye All!"
  end
end

To run the newly created test simply run rake in the terminal and watch the newly generated test fail with and error.

Run options: --seed 41493

# Running:

E

Fabulous run in 0.011996s, 83.3611 runs/s, 0.0000 assertions/s.

  1) Error:
HelloWorldDisplayedOnHomePage Feature Test#test_0001_the test is sound:
NameError: undefined local variable or method `root_path' for #<#<Class:0x007fbc7e44bf00>:0x007fbc7e402300>
    test/features/hello_world_home_page_test.rb:5:in `block (2 levels) in <top (required)>'

1 runs, 0 assertions, 0 failures, 1 errors, 0 skips

Thats its, we now have rails, minitest and capybara setup for BDDing your new app. Happy red, green, refactoring!!!

@brookr
Copy link

brookr commented May 30, 2014

Looks great! I'd simply delete this:

, if you are using rails 4.0.0 make sure to specify minitest ~> 5.??

Also, students have run into trouble with this line:
ENV["RAILS_ENV"] = "test"
and instead had better luck with:
Rails.env = 'test'

Next bundle your Gemfile and run the minitest installation generator
The generator is from the minitest-rails gem specifically.

This post will be SUPER helpful to the next round of students. :]

@gringocl
Copy link
Author

@brookr, the reason for the rails 4.0.0 comment is 4.0 is still using minitest 4.x, I guess it would be better just to specify 4.1+

I also looked up the Rails.env = 'test' it seems this was patched but is still a problem if you --skip-test-unit when you create a new rails app. https://github.com/blowmage/minitest-rails/issues/144.

Thanks for the input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment