Archive for April, 2009

Listing of cucumber's out-of-the-box webrat steps

Sunday, April 19th, 2009

Commonly used webrat steps found in cucumber

(replace words in [brackets] with the approprate word.)

Given

Given I am on [page_name]

When

When I go to [page_name]
When I press "[button]"
When I follow "[link]"
When I fill in "[field]" with "[value]"
When I select "[value]" from "[field]"
When I select "[time]" as the date and time				#datetime_select
When I select "[datetime]" as the "[datetime_label]" date and time	#specific datetime_select
When I select "[time]" as the time			#time select
When I select "[time]" as the "[time_label]" time	#specific time select
When I select "[date]" as the date			#date_select
When I select "[date]" as the "[date_label]" date	#specific date_select
When I check "[field]"
When I uncheck "[field]"
When I choose "[field]"
When I attach the file at "[path]" to "[field]"

Then

Then I should see "[text]"
Then I should not see "[text]"
Then the "[field]" field should contain "[value]"
Then the "[field]" field should not contain "[value]"
Then the "[label]" checkbox should be checked
Then I should be on [page_name]

Functional Testing Rails with a Session variable

Wednesday, April 8th, 2009

I recently had a frustrating situation while using the session to store the id of the current user. I was writing functional tests for my app an was using code based on the acts_as_authenticated plugin. My problem was that I was trying to set the id in the session before I made the get request and for whatever reason it wasn’t working and I kept getting redirected to the login screen.

Now I was not using the acts_as_authenticted_plugin, so

1
2
3
  def setup
    login_as :quentin
  end

was not available to me. Instead, I tried doing something along the lines of this, which mimics what it seemed that the authenticated_test_helper was doing:

1
2
3
4
test "that a request for index will be sucessful" do
  @request.session[:identity_id] = 1
  assert_response :success
end

but that didn’t work either. So I looked around a number of places and finally came across the newly shiny Rails Guide for functional testing. There, before my eyes, was the answer.

The get method kicks off the web request and populates the results into the response. It accepts 4 arguments:

  • The action of the controller you are requesting. This can be in the form of a string or a symbol.
  • An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
  • An optional hash of session variables to pass along with the request.
  • An optional hash of flash values.
  • Four arguments! not only the two I’d used (action and params), but 4. Armed with this knowledge, I soon got my test to pass. Here is a sample passing test

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    def setup
      @identity = Identity.make
    end
     
    test "that a request for index will be sucessful" do
       get :index, nil, build_session_hash_for(@identity)
       assert_response :success
    end
     
    protected
      def build_session_hash_for identity
        {'identity_id' => identity.id}
      end

    Hopefully this will help out someone else who may run into this issue.