Bdd
Bdd brings cucumber-style Given/When/Then/And/But steps for RSpec examples
Status
| Master | Gem Version |
|---|---|
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'
'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
Given "I log in" do
visit '/login'
fill_in 'Login', with: '[email protected]'
fill_in 'Password', with: 'password'
"Log in"
expect(page).to have_content('Welcome [email protected]')
end
end
def
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
given_I_log_in
When 'I update profile description' do
...
end
end
it 'User updates profile avatar' do
given_I_log_in
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 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'
"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_log_in_page
Given 'I am on the login page' do
visit '/login'
end
end
def when_I_submit_the_log_in_form
When 'I put credentials' do
fill_in 'Login', with: '[email protected]'
fill_in 'Password', with: 'password'
"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
Given "I log in" do
given_I_am_on_the_log_in_page
when_I_submit_the_log_in_form
then_I_should_be_logged_in
end
end
context 'User Flow' do
it 'User updates profile description' do
given_I_log_in
When 'I update profile description' do
...
end
end
it 'User updates profile avatar' do
given_I_log_in
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
Renaming
Useful for refactored nesting, you can change a step's name
def when_I_log_in
When "I log in" do
visit '/login'
fill_in 'Login', with: '[email protected]'
fill_in 'Password', with: 'password'
"Log in"
expect(page).to have_content('Welcome [email protected]')
end
end
def given_I_log_in
Given when_I_log_in
end
context 'User Flow'
it 'User updates profile description' do
given_I_log_in
When 'I update profile description' do
...
end
end
it 'User updates profile avatar' do
given_I_log_in
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
Development
Currently we only support RSpec
minitest and test_unit pull requests are wanted.
internationalization pull requests are wanted.
Authors
Contributing
- Fork it ( https://github.com/thejamespinto/bdd/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - 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.