Using Coulda

If you don't already, you should create a "test_helper.rb" file, or something similarly named, to DRY up your tests. In that helper file, you'll want to add the following:

    require 'rubygems'
    require 'coulda'
    include Coulda
The first line may be redundant if you (or something that you've already required) previously loaded rubygems.

Features

Coulda's take on BDD, based on Cucumber, groups behavior into "Features". A Feature defines its purpose and Scenarios that explain and exercise that purpose.

A pending Feature is a feature lacking any Scenarios. Below is an example of a pending Feature.

    require 'test_helper'
    
    Feature "A pending feature"

Below is an example of a Feature that contains a purpose.

    require 'test_helper'
    
    Feature "A pending feature" do
      in_order_to "accomplish something else"
      as_a "user"
      i_want_to "do something"
    end

Scenarios

A Scenario demonstrates one aspect of the Feature. Often, it's a happy or one of many sad paths exercising the behavior in our Feature.

You can write a "pending" Scenario using Coulda as follows:

    require 'test_helper'
    
    Feature "A pending feature" do
      in_order_to "accomplsih something else"
      as_a "user"
      i_want_to "do something"
    
      Scenario "that is pending"
    end

Coulda recommends that you express Scenarios using:

The most common use of Scenario would specify at least one Given, When, and Then perhaps as below:

    require 'test_helper'
    
    Feature "Painfully obvious" do
      in_order_to "demonstrate a simple test"
      as_a "coulda developer"
      i_want_to "provide a straight-forward scenario"
    
      Scenario "Describing something obvious" do
        Given "something without a value" do
          @no_value = nil
        end
    
        When "I give it a value" do
          @no_value = true
        end
    
        Then "it should have a value" do
          assert(@no_value)
        end
      end
    end

Reuse within a Feature

If you wish to reuse code within a Feature, you may want to factor behavior out into methods:

    require 'test_helper'
    
    Feature "Painfully obvious" do
      in_order_to "demonstrate a simple test"
      as_a "coulda developer"
      i_want_to "provide a straight-forward scenario"
    
      def something_without_a_value
        @no_value = nil
      end
    
      def self.this_method_is_called_by_name
      end
    
      Scenario "Describing something obvious" do
        Given "something without a value" do
          something_without_a_value
        end
    
        And :this_method_is_called_by_name
    
        When "I give it a value" do
          @no_value = true
        end
    
        Then "it should have a value" do
          assert(@no_value)
        end
      end
    end

Reuse between Features

However, if you wish to reuse code between Features, then you should put it in a Module and mix it in. The below example defines the Module within the same source file; however, it should be obvious that you can do so in a separate file and require it as desired.

    require 'test_helper'
    require 'coulda'
    include Coulda
    
    module MyMacros
      def given_something_without_a_value
        Given "something without a value" do
          @no_value = nil
        end
      end
    
      def assert_variable_has_a_value
        assert(@no_value)
      end
    end
    
    Feature "Painfully obvious" do
      extend MyMacros
    
      in_order_to "demonstrate a simple test"
      as_a "coulda developer"
      i_want_to "provide a straight-forward scenario"
    
    
      Scenario "Describing something obvious" do
        given_something_without_a_value
    
        When "I give it a value" do
          @no_value = true
        end
    
        Then "it should have a value" do
          assert_variable_has_a_value
        end
      end
    end