Integration Tests: Cucumber vs. RSpec
I’ve recently started writing integration tests in RSpec instead of Cucumber and I have been LOVING IT. It’s taken much less time and a lot less terrible code. Here is what I need for a test using Cucumber:
features/show_inventory.feature
Background: And I am signed in as a user And an inventory "I" exists Scenario: I can view the inventory page from the inventory list page Given I am on the inventories page When I click on the inventory row for that inventory "I" Then I should be on inventory "I"'s page
features/inventory_steps.rb
Given %r{^I am signed in as an? #{capture_model}$} do |model_name| user = create_model model_name login_with(user.email, user.password) @current_user = user end When %r{^I click on the inventory row for #{capture_model}$} do |model_name| inventory = find_model model_name App.inventories_index.list.find_inventory(inventory).click end
The steps that aren’t covered manually are covered using a tool called pickle. Pretty useful, but can also be sorta frustrating. We also use SitePrism for both styles of tests and if you aren’t using it, you should reconsider.
So what does an RSpec integration test look like? If I was covering the same simple problem, here’s what I would do:
spec/features/user_inventory_spec.rb
require 'rails_helper' RSpec.feature 'load inventory as user', :js do let(:user) { create :user } before do 3.times do create :inventory end end scenario 'select inventory and view the page' do Sessions::SessionsNew.sign_in user.email, 'my_test_pass' inventory = Inventory.first Inventories::InventoriesIndex.new.tap do |page| page.load page.list.find_inventory(inventory).click end Inventories::InventoriesShow.new.tap do |page| page.displayed? end end end
And that’s it. I just have to have the SitePrism pages for Sessions::SessionsNew, Inventories::InventoriesIndex, and Inventories::InventoriesShow designed correctly as well. But those I will use multiple times in other tests, so after the inital setup, I save a lot of time. In this case, I had already set the pages up while we were using Cucumber, so I didn’t have to do anything new to start writing tests in RSpec.
TL;DR unless you have a PM that is actually writing your Cucumber tests, just do them in RSpec. You’ll save yourself many a headache.