RSpec with Capybara testing javascript
Capybara is the latest testing library that is more robust than webrat. Capybara helps you test your web applications like how a real user would interact with your system.
It can test javascript functions with the :js => true option. Let’s say we have a view page that invokes an ajax function that returns a partial to the page on form submission. The older testing libraries could not/ would not test this as an ajax call, it would only test the controller interaction.
With capybara we can turn the :js => true option on the test case like:
[ruby] it “should see payment error if payment amount is smaller than product price”, :js = true do
login_js
find_link(“Canon Camera”).click
fill_in “payment_card_number”, :with => “1234567891234567″
fill_in “payment_card_name”, :with => “validname”
click_button(‘pay_now’)
page.should have_content(“There was a problem saving your order”)
end
[/ruby]
Here a user logs in then clicks on a product (let’s say in an ordering application). He/she fills in the payment details and we have an ajax call which returns a partial. This can be well tested with the option above.