Monday, February 20, 2012

Ruby on Rails: Implementing should_not_have_content in CapyBara

CapyBara is an awesome tool for testing purpose. It provides you way to apply BDD while writing Rails code.

In Manning's Rails3 in Action. In Chapter 4 one of the rules is:

And I should not see "Standards compliance"
The whole view_tickets.feature file is given below:


Feature: Viewing tickets
    In order to view the tickets for a project
    As a user
    I want to see them on that project's page
Background:
    Given there is a project called "TextMate 2"
    And that project has a ticket:
    | title | description |
    | Make it shiny! | Gradients! Starbursts! Oh my! |
    And there is a project called "Internet Explorer"
    And that project has a ticket:
    | title | description |
    | Standards compliance | Isn’t a joke. |
    And I am on the homepage
Scenario: Viewing tickets for a given project
    When I follow "TextMate 2"
    Then I should see "Make it shiny!"
    And I should not see "Standards compliance"
    When I follow "Make it shiny!"
    Then I should see "Make it shiny" within "#ticket h2"
    And I should see "Gradients! Starbursts! Oh my!"
    When I follow "Ticketee"
    And I follow "Internet Explorer"
    Then I should see "Standards compliance"
    And I should not see "Make it shiny!"
    When I follow "Standards compliance"
    Then I should see "Standards compliance" within "#ticket h2"
    And I should see "Isn't a joke."


In order to implement Should not have , you just apply following simple step in steps file:

Then /^I should not see "([^"]*)"$/ do |arg1|
  page.should_not( have_content(arg1))
end 

Upon running cucumber:ok command it should execute And I should not see "Standards compliance" or any other string matches the criteria properly