Posts tagged testing
Testing for a redirect using Capybara and Selenium WebDriver
1I just spent the better part of 8 hours trying to figure out how to do this. The Selenium WebDriver API docs are pretty bewildering especially when it comes to trying to interact with the HTTP response or request itself because those constructs are abstracted quite a bit.
1 2 3 4 5 6 7 8 9 10 11 | Then /^I should be on the (.*) page$/ do |page_name| object = instance_variable_get("@#{page_name}") page.current_path.should == send("#{page_name.downcase.gsub(' ','_')}_path", object) page.status_code.should == 200 end Then /^I should be redirected to the (.*) page$/ do |page_name| page.driver.request.env['HTTP_REFERER'].should_not be_nil page.driver.request.env['HTTP_REFERER'].should_not == page.current_url step %Q(I should be on the #{page_name} page) end |
Which we can use in a Cucumber feature like so:
1 2 3 4 5 6 | Given I am on the homepage When I click on the link to add a widget Then I should be able to complete the widget form And I should be redirected to the widget page And I should see a confirmation message And I should see the widget listed |
Testing model validations in rspec the short and sweet way
5There seems to be a divide in the Rails/TDD community over whether you should bother testing your model validations. On the one hand, it’s silly to just retest what the Rails core team has hopefully already tested — that validations work. On the other hand, I want to make sure that my models only accept the data that I design them to accept. I also tend to use my RSpec unit tests as blueprints for building out my models, in true TDD fashion, so I start out by defining what values they should accept.