Tuesday, November 22, 2011

"WARNING: Cucumber-rails required outside of env.rb. The rest of loading is being defered until env.rb is called." with Bundler prerelease (1.1)

Had a real 'fun' time (ie. not fun) getting around this error every time I tried to run 'bundle exec spork cucumber' (which would then die):
Using Cucumber

Preloading Rails environment
WARNING: Cucumber-rails required outside of env.rb.  The rest of loading is being defered until env.rb is called.
  To avoid this warning, move 'gem cucumber-rails' under only group :test in your Gemfile
Loading Spork.prefork block...
undefined method `allow_rescue=' for ActionController::Base:Class (NoMethodError)

I couldn't find any erroneous require's of cucumber-rails outside my :test group in my Gemfile, but eventually I stumbled across the solution in the comments of this pull request (for a separate issue).

I had to change the line in my Gemfile to:

gem "cucumber-rails", "~> 1.0", require: false

For the error message to go away, and spork to run properly. It seems to have been caused by a way the new (prerelease/1.1) version of Bundler loads gems - what a bitch (but it is a lot faster than the older versions, so I'm still using it).

Wednesday, November 16, 2011

Testing objects in Cucumber

it always takes me a search and a few clicks to find this article on how to use FactoryGirl with Cucumber: https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md

not. anymore.

Monday, November 14, 2011

WARNING: i18n methods within step definitions are deprecated

When running some older step definition files with a newer version of Cukes, I kept getting the message "WARNING: i18n methods within step definitions are deprecated..." even though I couldn't see any il8n methods in my cucumber files.

After a bit of digging, I found that you need to update your steps files so that you don't use any And/Then/When/Given methods inside your steps, and instead use the method step - but this only applies inside your step definitions. For example this:

When /^I sign in as "(.*)\/(.*)"$/ do |email, password|
  Given %{I am not logged in}
  When %{I go to the sign in page}
  And %{I fill in "Email" with "#{email}"}
  And %{I fill in "Password" with "#{password}"}
  And %{I press "Sign in"}

Changes to:

When /^I sign in as "(.*)\/(.*)"$/ do |email, password|
  step %{I am not logged in}
  step %{I go to the sign in page}
  step %{I fill in "Email" with "#{email}"}
  step %{I fill in "Password" with "#{password}"}
  step %{I press "Sign in"}

Saturday, November 5, 2011

Using GitHub to get better at Ruby

Aside from being a great site to host open source software, GitHub is a great place to review other people's code. By following all the repositories you're interested in, you get to see all their latest updates on your dashboard when you log in. I've found it useful for writing better specs/scenarios - when I get writer's block, I go look at some of the bigger Ruby-based projects on Github for ideas.

Another cool thing to do is search repos for specific gems/libraries that you are interested in using, or trying to get your head around. When you search GitHub, you can do a search with very specific parameters.

For example: Search "Code" with language "Ruby" and term "require savon" to see other projects using the Savon gem - because consuming SOAP services in Ruby are so much fun! I mean, it's almost as good as getting teeth pulled... (Savon makes it bearable)

Friday, November 4, 2011

Rails 3.1, Spree, and "rake aborted! Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes"

I keep getting the following error when trying to run Rails 3.1 with Spree commerce, right when running my first rake command 'rake db:create':
$ rake db:create

rake aborted!
Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

(See full trace by running task with --trace)

The solution is to manually add the following gems to your gem file (and run the obligatory 'bundle install' afterwards):
gem 'execjs'
gem 'therubyracer'

Hope that helps.

Wednesday, November 2, 2011

Bundle Install gives Gem::Exception: Cannot load gem at

While trying to install someone else's Rails 3.0.x app on my dev server, the 'bundle install' command kept failing, with an error about the cache location for gems.
$ bundle install

Fetching source index for http://rubygems.org/

Gem::Exception: Cannot load gem at [/usr/lib/ruby/gems/1.9.1/cache/rake-0.8.7.gem] in /home/rowan/newprj
An error occured while installing rake (0.8.7), and Bundler cannot continue.
Make sure that `gem install rake -v '0.8.7'` succeeds before bundling.

After reading a StackOverflow post on a similar error message I found that the way around it is to do a bundle install as root:
$ sudo bundle install

This isn't the right way to do it normally, and I don't recommend it, but it does seem to be necessary at the moment because Ubuntu (11.x) has Ruby 1.9.2 packaged under the 1.9.1 label/location.

Tuesday, November 1, 2011

We don't need no stinkin' web steps!

I spent my first attempts back in BDD-land wondering why all the examples I was reading kept referring to Capybara's web_steps.rb file, and why I didn't have it. It all made sense when I finally found this article by Aslak.

The file web_steps.rb was originally in Webrat (which is what I used last time I was doing Rails apps in Rails 2.3). It contained a bunch of pre-defined, regex-dependent steps which could be incorporated in to your scenarios for quick development of - but ultimately limiting - your scenarios.

Read the article for the full reasons behind it. It seems a bit of an inconvenience for someone starting out, but seeing as Aslak created Cucumber, I'm not going to argue with him about the finer points of BDD.

The Capybara API is what you should be using instead to replace web_steps.rb functionality in the future.