u52tsw
Last Updated: February 25, 2016
·
563
· nicolaslazartekaqui

Rspec - Getting “by” with rspec feature specs

Sometimes we have many responsible for an action that will be tested, look this

it "allows the user to manage her account" do
  #Login user
  visit "/login"
  fill_in "Username", :with => "jdoe"
  fill_in "Password", :with => "secret"
  click_button "Log in"
  expect(page).to have_selector(".username", :text => "jdoe")

  #Account management
  click_link "Account"
  expect(page).to have_content("User Profile")
end

we can be more semantic using a method by, in spec_helper.rb

def by(message)
  if block_given?
    yield
  else
    pending message
  end
end

alias and_by by

and them rewrite test

it "allows the user to manage her account" do
  by "logging in the user" do
    visit "/login"
    fill_in "Username", :with => "jdoe"
    fill_in "Password", :with => "secret"
    click_button "Log in"
    expect(page).to have_selector(".username", :text => "jdoe")
  end

  and_by "managing the account" do
    click_link "Account"
    expect(page).to have_content("User Profile")
  end
end

Originally published by Pivotal Labs

http://pivotallabs.com/getting-by-with-rspec-feature-specs/