Code Snippets
Small snippets of code that I find interesting or useful. Generally my own work, but I will cite sources if not.
Testing for a redirect using Capybara and Selenium WebDriver
0I 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.
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:
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
2There 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.
Named redirect routes in Rails 3
0I didn’t see this mentioned anywhere in the Rails Guide or in my admittedly cursory Google searches, but apparently it’s possible to create named redirect routes in Rails 3. For example:
| 1 2 3 4 |
|
Then you can use those in your layouts like so:
| 1 2 3 4 |
|
It Should Include Only One Of – An RSpec Matcher
0I know RSpec tests should typically be implementation/data agnostic, but I ran into a situation in a unit test where I needed to make sure that the results of a query returned only one of the two records that were created during the setup but without knowing which record that would be ahead of time. This was due to the fact that the scoped finder returned a random record. So, I wrote a little matcher that lets me assert that a given array contains only one of a set of specified elements.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
You can use it like so:
| 1 2 3 4 5 6 7 8 9 |
|
Join (implode) an Array of String Values with Formatting
1In programming, I often need to join values together from an array. Typically this will be to do something on the backend, like join a list of integers together in a comma-delimited string for use in a SQL statement. For those times, the native join/implode functions work fine. Occasionally however, we need to finesse the resulting string to look a little more readable. I wrote a small function to do so. It’s called pretty_join()
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Given an array of 0 or more values, pretty_join() will concatenate all of the values together using a common delimiter (a comma by default) except for the last two values which will be joined by the final separator (an ampersand by default).
Examples:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Find the longest common substring using PHP
1I recently found a need to find the longest common substring in an array of strings in PHP. A couple of Google searches didn’t return any relevant solutions, so I decided to roll my own. I haven’t benchmarked this yet for large strings and/or arrays, but it does what I needed it to for my own purpose.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Example:
| 1 2 3 4 5 6 7 8 9 10 |
|
Recent Comments