Pow, meet rbenv

I just ran into a problem with Pow when using rbenv for Ruby version management whereby Rails apps were not launching properly, giving the the following error message:

Error starting application
Your Rack app raised an exception when Pow tried to run it.

Bundler::GemNotFound: Could not find ??? in any of the sources

Turns out that Pow doesn’t know anything about rbenv unless you tell it. Thomas Fuchs tweeted a solution, suggesting that you just add the following to a ~/.powconfig file:

export PATH="/Users/username/.rbenv/shims:/Users/username/.rbenv/bin:$PATH"

Kill off your Pow process (I use the Powder gem for this i.e. powder down && powder up) and all should work just fine.

I personally prefer the solution mentioned in this related Github ticket as is saves having to hardcode your home directory:

export PATH=$(rbenv root)/shims:$(rbenv root)/bin:$PATH

Happy Pow’ing.

Using both Pow and Apache on OSX Lion

I have written previously about using Pow for Ruby/Rails development. Pow is great if you are purely developing in Ruby, however I’ve recently found myself needing to edit a WordPress site.

Pow intercepts requests to port 80 by default meaning that no requests can make it through to the default Apache installation on OSX. I’ve found the best way around this is to disable Pow whenever developing in PHP. The powder gem handles this nicely, so:

gem install powder

Now you can now disable Pow with the following, therefore letting requests through to Apache (assuming you have it enabled):

powder down

And once you’re done with the PHP development, enable it again with:

powder up

As a side note, I usually leave Apache disabled by default i.e. uncheck “Web Sharing” within the “Sharing” section of System Preferences until I am going to be doing some PHP development.

Hope this saves someone some time figuring out how to disable/enable Pow.

Loving MongoDB but Missing Transactions

I’ve been venturing into the world of MongoDB via Mongoid in a Rails app. On one hand it’s a breath of fresh air (no migrations, flexible schemas etc.) but on the other hand I really, really miss transactions.

If somebody asked me if I used transactions much in MySQL, I would probably have said no… however, now that I don’t have them I realise just how much I used them. Things that just work when using ActiveRecord such as creating a record and ensuring that an associated record gets created is sooo much harder.

MongoDB has “Atomic Operations” which means if you have an embedded relationship, all is good and the database will be rolled back if the entire document does not save. As soon as you have two top level collections and need to ensure that a record is created in both, things get a little hairy.

There seems to be a great deal of “if you need transactions just use a RDBMS” talk but what if you need a flexible schema and transactions, the last thing you want to manage is some kind of EAV system like Magento.

I’ve created a thread on the Mongo User forum with a typical example so feel free to join in the discussion. I’d love to hear experienced Mongo user’s views on how to overcome these kinds of situations and what kind of applications people are running with MongoDB.

Using Pow when developing Rails applications on OSX

I heard about Pow a while ago which is “a zero-config Rack server for Mac OS X” but never got around to giving it a go. I’ve just installed it and can instantly see that it is going to make developing with Rails (or any Rack app) a whole lot nicer, especially when dealing with apps that use subdomain account keys.

If you’ve not tried it yet, head over to the Pow website and get it installed with:

curl get.pow.cx | sh

Happy new year everyone!

Fixing {{model}} placeholders in Rails 2.3 when Rails 3 is installed

This was causing me all kinds of grief. I am running Rails 2.3 and Rails 3 apps on the same server and on installing Rails 3, the Rails 2.3 apps started displaying {{model}} and similar strings in views.

It turns out that Rails 3 uses i18n version 0.5.0 and with this version as far as I can see, the placeholder text in translations follow the %{text} format instead of the {{text}} format in 0.4.2 which Rails 2.3 was using. The only way I found to fix this was to declare the specific i18n version before the Rails gem gets required in the “load_rails_gem” method of “config/boot.rb” in my Rails 2.3 apps:

def load_rails_gem
  gem 'i18n', '0.4.2' # Add this line

  if version...

Doing this specifies that we want i18n 0.4.2 exactly, whereas Rails 2.3 specifies version “>= 0.1.3″ which would obviously includes “0.5.0”.

In running Rails 2.3 and Rails 3 apps side by side, I also ran into this problem and overcame it with the fixes by “bct” and “ronin-95281” in the comments… apparently Rails Core won’t fix this.

If anyone else has a cleaner fix for this I would love to hear about it.

Ruby Causing MacBook Pro to Run Hot

The past couple of days the fan on my MacBook Pro has been constantly on and battery usage down to around a third. Launching Activity Monitor, it showed 3 Ruby processes all at 100% CPU usage. The solution was to force quit these processes and within seconds, the fan slowed up and the battery indicator went up. Force quitting these processes didn’t affect anything I was doing with Rails or IRB so I guess they were just stray processes.

If anyone else gets the same problem, just launch Activity Monitor which lives in the /Applications/Utilities folder, click the CPU tab if not already selected, select processes with a process name of “ruby”, click “Quit Process” and then select “Force Quit”. Just selecting “Quit” wouldn’t work for me hence the “Force Quit”.

I hope this saves someone else the head-scratching as to why their Mac is running hot.

Using Artifice to Stub Server Responses

In a recent project I needed a way to fake a response from a server in my Cucumber features. Specifically, I was testing integration with a payment gateway and wanted to stub it’s responses based on different requests.

In the past I have used FakeWeb, however it becomes a little hairy when you need to stub a response based on request body. I came across a couple of alternatives, firstly WebMock which looks promising but then Artifice from the mighty Yehuda Katz caught my eye…

Artifice lets you “replace the Net::HTTP subsystem of Ruby with an equivalent that routes all requests to a Rack application”. I like the simplicity of this solution as you can in essence use a Rack application to replace the responses of the service you are testing.

An Example

First we create a simple Rack application to stand in for the server we are interacting with.

app = proc do |env|
  [200, { "Content-Type"  => "text/html" },
    ["Hello world: #{env.inspect}"]

Then we simply use Artifice’s “activate_with” method to wrap any requests.

Artifice.activate_with(app) do
  response = Net::HTTP.start("google.com") do |http|
    http.post("/the_url", "foo=bar")
  puts response.body

This allows for a Rack app to be used as a stand in for a complete API, it could be a Sinatra app for example allowing for easy route handling. We could go so far as to have a series of Rack apps that can be used as stand-ins for common API’s.

“Ramble”, the Javascript Cucumber Port (work in progress)

I am a great fan of Cucumber when it comes to integration testing, however testing heavy use of Javascript can be a little tedious.

I have looked into the different solutions out there such as Selenium but found them all to be fiddly to setup, however Capybara helps on this front. I was thinking, what if Cucumber could run in the browser? No need for Javascript adapters or XML parsers, Safari/Chrome/Firefox already do a great job of this. Manipulating the page such as filling in forms, clicking links etc. could all be done with jQuery, in a very concise manor.

I decided to create a proof-of-concept while it was still fresh in my head. This is by no means a fully working release and the code leaves a lot to be desired in it’s current state, however it shows the benefits of a “Cucumber in the browser”. The main benefits I can see so far (for both javascript and non javascript apps) are:

  • Speed – browsers are getting extremely quick at this DOM stuff.
  • Flexibility – everything happens client-side meaning you can easily test with any server technology.
  • Simplicity – no need for complex javascript adapters, XML parsers etc.

The basic file structure is very similar to Cucumber:

  - features
    - js
    - steps
    - support

Step definitions can be defined in plain old javascript files with plain old jQuery, in this case web-steps.js. Currently the step definition are expected to throw an error if they cannot be fulfilled, this may change when a solid API is nailed down:

  // The value of 'this' is the current document as a jQuery object.
  ramble.match(/^I follow "(.+)"$/, function(link_text) {
    var link = this.find('a').filter(function() { return $(this).text() == link_text; });
    if(!link.length) throw("Can't find link: " + link_text);

Scenarios are exactly the same as in Cucumber, so you can do something like the following:

  Scenario: User fill out a form
    Given I am on the homepage
    And I follow "Tell us your name"
    And I fill in "First name" with "Jamie"
    And I fill in "Last name" with "Hill"
    And I press "Submit"
    Then I should see "Thank you for your details."

You can now simply drop the features folder into the public area of your app and visit the url in your browser. You should see the relevant steps go green or red as the app is navigated in an iFrame.

I plan to first tidy up the API (and unit test) and then aid the writing of scenarios by adding the ability to record them within the browser (Selenium style).

I’d like to hear peoples views on this… please don’t be too hard on the code, it’s more of a mind-dump than anything else at this stage (around 100 lines). There is an example of testing a static site included so just load the features/index.html file in your browser to see it run.


Update 04/07/10: As noted by Andrew in the comments, you will need a server running in order for browsers to get access to the pages for testing.

I have added a simple server script allowing the features to be run locally (requires Ruby). If you want to see it in action, just run:

cd /path/to/ramble/checkout
ruby server.rb

…and then visit http://localhost:1234/features in your browser (tested in Firefox, Chrome and Safari). Note that Ramble is not at all dependent on Ruby, it is just used for running a local test server.


Ramble in action

Nokogiri for Cucumber steps that need to compare HTML attributes

I have found on numerous occasions that I need to look at HTML attributes when diffing tables etc. in Cucumber. My current need for this arrises as I have a gallery of thumbnails that all have alt tags who’s values I need to compare as there is not a text equivalent.

A feature statement like this:

Then I should see the following thumbnails
  | Mona Lisa  |
  | Sunflowers |

Could be achieved with the following step:

Then /^I should see the following thumbnails$/ do |table|
  nodes = Nokogiri::HTML(response.body).css('ul#gallery li img')
  table.diff!(nodes.map { |img| [img.attributes['alt'].to_s] })