Being careful with Cucumber step names

Today I had an interesting problem with Cucumber the step “I sign in” was being called when my feature file had “When I sign in to the ….”.

The step looked like this:

When /I sign in/ do
    visit “/signin”

    …
end

Turns out, this is actually acceptance – and nice behaviour! The reason is because the regex doesn’t state start end characters, as such it would make to any step with the text “I sign in”

Why is it nice? Well I also had two step definitions called

Given I do something
And I do something again

Both did exactly the same thing, but to make the feature more readable I added the again word. Sadly the implementation looked something like this with the step being duplicated.

Given /^do something again$/ do
  Given ‘do something’
end

With the trick above, I could simply have the step below which would be called with and without the again word.

Given /^do something/ do
   # Work goes here.

end

Simple, but effective.

Which Ruby gems are loaded?

I’ve just been trying to debug an issue with a Ruby gem monkey patching methods which resulted in a bug.

It turns out that it’s easy to find out which gems loaded at a particular point within your application by simply including the following line:

puts Gem.loaded_specs.keys

This happily outputs the loaded gems, sadly it didn’t help me find the problem but did rule out a possible cause.

Installing Rails 3.0, Ruby 1.9.2 and Pik on Windows

As you may have heard, Rails 3.0 final and Ruby 1.9.2 have been released. These have got a large number of features, with both representing the next stage of Ruby development.

While Rails 3.0 won’t cause too many problems, it does require Ruby 1.8.7 or higher. As Ruby has grown, multiple versions have been released which can live happily side-by-side. Sadly, command lines don’t make it easy to specify which version you want to execute.

For example, I have two versions of MSBuild on my machine with my command prompt being aware of both:

>where msbuild
C:WindowsMicrosoft.NETFrameworkv4.0.30319MSBuild.exe
C:WindowsMicrosoft.NETFrameworkv3.5MSBuild.exe

However, by default, it will execute the one it finds first when scanning the directories set in the PATH:

>msbuild
Microsoft (R) Build Engine Version 4.0.30319.1

The same is true with Ruby. Yet, this is complicated by the fact that there are multiple different executables (gem, rake, cucumber, spec, rails etc) together with different gems depending on the core Ruby version – 1.8 and 1.9 will have different gems installed even on the same machine. 

To help manage this, the Ruby Version Manager (RVM) was created. “RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems”

Sadly, RVM doesn’t work on Windows. Thankfully, Pik does – “Pik is a tool to manage multiple versions of ruby on Windows”.

This means we can do the following:

>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

>pik switch 192

>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

Below is how I installed Pik, along with Ruby 1.9.2 and Rails 3.0 to start the next part of my Ruby development journey.

Installing Pik

My machine already has Ruby 1.8.6 (ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]) installed which means I can install pik via RubyGems. If you don’t already have Ruby, the README file on GitHub has a section called “Install pik using the installer“ which I recommend you follow.

gem install pik

After downloading the gem, you need to install pik into a directory on your machine. This can be anywhere, apart from where you already have Ruby installed (C:rubybin).

>mkdir C:pik

You need to include the directory used above in your %PATH% environment variable before the location of any existing Ruby installation. I put it at the start.

When installing, simply specify the directory you picked.

>pik_install C:pik
Thank you for using pik.

After which, you will have three files in the folder. This is everything required for pik.

29/08/2010  17:08               119 pik.bat
29/08/2010  17:08               145 pik.ps1
29/08/2010  17:08           694,272 pik_runner.exe

Using Pik

I can now start working with Pik. Executing the pik command will locate all existing Ruby installations and configure itself.

>pik
** Adding:  186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
Located at:  C:Rubybin
Usage: pik command [options]

Executing pik list outputs all the ruby installations it knows about. 

>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

As Rails 3.0 requires 1.8.7 or higher, let’s install the latest version of Ruby.

>pik install ruby
** Downloading: 
http://rubyforge.org/frs/download.php/71175/ruby-1.9.2dev-preview3-i386-mingw32-1.7z
   to:  C:UsersBen Hall.pikdownloadsruby-1.9.2dev-preview3-i386-mingw32-1.7z

Sadly, this still tried to install a dev preview, even with the latest version released. Luckily, it can be installed manually.

Manually installing Ruby 1.9.2

By default, installing Ruby on Windows can be somewhat difficult. Thankfully, a 1.9.2 one click installer has been released which you can download here – rubyinstaller-1.9.2-p0.exe

After clicking next a few times, I installed Ruby into C:Ruby192.

I then need to tell pik about my installation by pointing it at the bin directory. That’s it.

>pik add C:Ruby192bin
** Adding:  192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]
Located at:  C:Ruby192bin

If you list pik, then you can see all the different versions installed:

>pik list
* 186: ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
  192: ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

You can then switch to the particular version you want, for example originally I was running 1.8.6.

>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]

But I can then switch to 1.9.2 with a simple command.

>pik switch 192

>ruby -v
ruby 1.9.2p0 (2010-08-18) [i386-mingw32]

Installing Rails 3.0

That’s the hard bit done. I didn’t have install pik but during the transition between 1.8.6 and 1.9.2 I *think* this will be invaluable. With 1.9.2 now configured and set as my current pik environment I can just install rails.

>gem install rails

Yep, that’s it. 

Rails 3.0 Hello World!!

To create a new rails app, let’s just execute the command:

>rails new HelloWorld3 

Followed by:

>rails server
=> Booting WEBrick
=> Rails 3.0.0 application starting in development on
http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-08-29 18:01:27] INFO  WEBrick 1.3.1
[2010-08-29 18:01:27] INFO  ruby 1.9.2 (2010-08-18) [i386-mingw32]
[2010-08-29 18:01:27] INFO  WEBrick::HTTPServer#start: pid=7472 port=3000

I now have our Rails 3.0 application running on top of Ruby 1.9.2 on Windows.

image

If I ever need to go back to Rails 2.3.5, I just type the following:

>pik default

>rails -v
Rails 2.3.5

Installing Cucumber 0.8.5 fails due to gherkin not installed

This morning I was attempting to install the latest version of Cucumber, however I recieved an error saying gherkin (the language parser of the tests) was not installed.

C:>gem install cucumber –no-ri –no-rdoc
ERROR:  Error installing cucumber:
        cucumber requires gherkin (~> 2.1.4, runtime)

Generally, gems install all of the dependencies so I found this a little bit strange. Naturally, I manually install it.

C:>gem install gherkin –no-ri –no-rdoc
Successfully installed gherkin-2.2.0-x86-mswin32
1 gem installed

Sadly, this still didn’t work. The reason was it needs 2.1.4 installed, not the 2.2 version.

Executing the following allowed me to install Cucumber as normal

gem install gherkin –version 2.1.4 –no-ri –no-rdoc

Time for a new stubbing and mocking syntax?

When using IronRuby to test C# applications we are still faced with similar issues as with C# – the different is how we can handle them. For example, to stub the HttpRequestBase in C#, we could use Rhino Mocks as follows.

var stubbedHttpRequest = MockRepository.GenerateStub().Stub(x=>x.ApplicationPath).Return(“~/test”);

I do really like this syntax and think it works for C#.  However, if we are looking to use Ruby and a dynamic language we have the potential to be more inventive.

IronRuby has an excellent framework called Caricature which allows you to fake CLR objects. For example, here we are stubbing the HttpRequestBase from MVC.

require 'caricature'
include Caricature
stubHttpRequest = isolate System::Web::HttpRequestBase     
stubHttpRequest.when_receiving(:application_path).return("~/test")    
stubHttpRequest 

However, this got me thinking. With Ruby being dynamic, how could we take advantage when defining fakes? For example, what about the following syntax:

stubHttpRequest = stub 'System::Web::HttpRequestBase
                          .ApplicationPath.returns("~/test") &&
                          .FilePath.returns("")'

This would stub two properties, ApplicationPath and FilePath to return “~/test” and an empty string respectively. If we wanted to handle method calls and arguments, we could have the following:

stubHttpRequest = stub 'System::Web::HttpRequestBase
                          .SomeMethodCall("WithArgument").returns(SomeObject.new) && 
                          .SomeOtherMethod(*).returns(-1)'

Here we stub two methods, one stubs with a particular argument (must be the string “WithArgument”) while the other matches on any argument.

My aim is to reduce the ceremony associated with the act of stubbing and instead focus on the true intent of the defined behaviour.

Note: Imagine the ‘refactoring’ problem has been solved, and changing the method names would also update the tests.

If we look at other languages, for example Javascript’s jqMock and Ruby’s NotAMock are using a similar syntax to C#.

var alertMock = new jqMock.Mock(window, "alert");
alertMock.modify().args("hello world new!").returnValue();

I think it is time to start looking beyond the existing syntax and reveal our true intent. What do you think?

RSpec, IronRuby and RubyMine

RSpec, IronRuby and RubyMine is an amazing combination. Here is a screenshot of an example I’m currently working on…

Rubymine_tests_passing

All I do is execute the Rake command below, which exists in my rakefile.rb. Rubymine does the rest! Very cool.

desc "Run all examples"
Spec::Rake::SpecTask.new('examples') do |t|
  t.spec_files = FileList['examples/**/*.rb']
end

RubyMine – The Meta keyboard shourtcut on Windows

Today I installed RubyMine 2.0 which is a great IDE from JetBrains. However, I was confused as all the keystrokes referred to ‘Meta’, for example ‘Meta+C’ for copying. Being on Windows 7, this wasn’t a valid keystroke.

After a bit of search around, it turns out RubyMine has keystrokes targeted for different environments, in my case it was targeted to be the same as TextMate. Awesome when I’m on my Mac, not so good on Windows.

To change the keymapping, select File > Settings. Pick the keymap option, and in the dropdown change it to Visual Studio.

image

Everything will then feel right at home.

no such file to load — mkmf

Today I was attempting to install a package on my ubuntu machine and I received the following error:

extconf.rb:1in 'require': no such file to load -- mkmf (LoadError)

To solve the problem, I simply installed the ruby1.8-dev package, this was done by the command:

sudo apt-get install ruby1.8-dev

I could then happily install the package I wanted.