Bdd

Bdd brings cucumber-style Given/When/Then/And/But steps for RSpec examples

Status

Master Gem Version
Master Build Status Gem Version

GitHub License Gem Downloads GitHub Issues

Description

This gem brings two major functionality to your tests

  • Verbosity for rspec documentation formatter.
  • Ability to comment or describe set of actions in example into some step.

Installation

Include in your Gemfile:

group :test do
  gem 'bdd'
end

RSpec

Run specs with custom format specified inline:

rspec --format Bdd::RSpec::Formatter --color spec

Or, if you want to use as your default formatter, simply put the options in your .rspec file:

.rspec

--format Bdd::RSpec::Formatter
--color

Output Example

spec/features/search_spec.rb

context 'Searching' do
  it 'Result is found' do
    Given 'I am on the search page' do
      visit '/search'
      expect(page).to have_content('Search')
    end

    When 'I search something' do
      fill_in 'Search', with: 'John'
      click_button 'Go'
    end

    Then 'I should see the word result' do
      expect(page).to have_content('Result')
    end
  end
end

Documentation formatting output:

rspec -fd spec/features/search_spec.rb


<b>Searching</b>
  <b>Result is found</b>
    <b>Given</b> I am on the search page
    <b> When</b> I search something
    <b> Then</b> I should see the word result

Shared Steps

Basic Example with shared steps

You can refactor steps into methods using plain Ruby syntax.

def 
  Given "I log in" do
    visit '/login'
    fill_in 'Login', with: '[email protected]'
    fill_in 'Password', with: 'password'
    click_button "Log in"
    expect(page).to have_content('Welcome [email protected]')
  end
end

def then_I_should_see_a_confirmation_message
  Then "I should see a confirmation message" do
    expect(page).to have_content('Your profile was updated successfully')
  end
end

context 'User Flow' do
  it 'User updates profile description' do
    
    When 'I update profile description' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end

  it 'User updates profile avatar' do
    
    When 'I update profile avatar' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end
end

Output:


<b>User Flow</b>
  <b>User updates profile description</b>
    <b>Given</b> I log in
    <b> When</b> I update profile description
    <b> Then</b> I should see a confirmation message

Nested Example 1

Outside of the scope

Given is automatically inserted as a before Then is automatically inserted as an after

Given "I log in" do
  visit '/login'
  fill_in 'Login', with: '[email protected]'
  fill_in 'Password', with: 'password'
  click_button "Log in"
  expect(page).to have_content('Welcome [email protected]')
end

Then "I should see a confirmation message" do
  expect(page).to have_content('Your profile was updated successfully')
end

context 'User Flow' do
  it 'User updates profile description' do
    When 'I update profile description' do
      ...
    end
  end

  it 'User updates profile avatar' do
    When 'I update profile avatar' do
      ...
    end
  end
end

Output:


<b>User Flow</b>
  <b>User updates profile description</b>
    <b>Given</b> I log in
    <b> When</b> I update profile description
    <b> Then</b> I should see a confirmation message

Nested Example 2

Nesting will silence any output from the internal steps

def 
  Given 'I am on the login page' do
    visit '/login'
  end
end

def 
  When 'I put credentials' do
    fill_in 'Login', with: '[email protected]'
    fill_in 'Password', with: 'password'
    click_button "Log in"
  end
end

def then_I_should_be_logged_in
  Then 'I should be logged in' do
    expect(page).to have_content('Welcome [email protected]')
  end
end

def 
  Given "I log in" do
    
    
    then_I_should_be_logged_in
  end
end

context 'User Flow' do
  it 'User updates profile description' do
    
    When 'I update profile description' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end

  it 'User updates profile avatar' do
    
    When 'I update profile avatar' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end
end

Output:


<b>User Flow</b>
  <b>User updates profile description</b>
    <b>Given</b> I log in
    <b> When</b> I update profile description
    <b> Then</b> I should see a confirmation message

Renaming

Useful for refactored nesting, you can change a step's name

def 
  When "I log in" do
    visit '/login'
    fill_in 'Login', with: '[email protected]'
    fill_in 'Password', with: 'password'
    click_button "Log in"
    expect(page).to have_content('Welcome [email protected]')
  end
end


def 
  Given 
end

context 'User Flow'
  it 'User updates profile description' do
    
    When 'I update profile description' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end

  it 'User updates profile avatar' do
    
    When 'I update profile avatar' do
      ...
    end
    then_I_should_see_a_confirmation_message
  end
end

Output:


<b>User Flow</b>
  <b>User updates profile description</b>
    <b>Given</b> I log in
    <b> When</b> I update profile description
    <b> Then</b> I should see a confirmation message

Development

Currently we only support RSpec

minitest and test_unit pull requests are wanted.

internationalization pull requests are wanted.

Authors

Contributing

  1. Fork it ( https://github.com/thejamespinto/bdd/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Alternatives and Inspiration


  <b>rspec-steps</b>, <b>rspec-given</b> and <b>rspec-example_steps</b> run <i>AS</i> examples.
  <b>bdd</b> and <b>cucumber</b> run <i>INSIDE</i> examples, running tests faster.