Here are some of the testing related tips I use while working on Spree.

Stop in rails console if something fails in cucumber test

If a cucumber test fails then one of the first things I check for is the state of current data. In order to do that I used to put debugger statements. I still use this technique but here is a much better approach.

Create a file called stop.rb and put this file at features/support/stop.rb.

The contents of stop.rb are as follows:

  
    # Usage:
    #  @wip @stop
    #  Scenario: change password
    #    ........................
    # $ cucumber -p wip
    After do |scenario|
      if scenario.failed? && scenario.source_tag_names.include?("@wip") && scenario.source_tag_names.include?("@stop")
        puts "Scenario failed. You are in rails console becuase of @stop. Type exit when you are done"
        require 'irb'
        require 'irb/completion'
        ARGV.clear
        IRB.start
      end
    end
  

At the end of features/support/env.rb add the following line

  
    require File.expand_path(File.dirname(__FILE__) + '/stop')
  

As mentioned in the Usage above, add tags @wip and @stop to a scenario. Now execute the cucumber tests using following command.

  
    $ cucumber -p wip
  

If your cucumber test fails then you will be taken to a rails console where you can do things like

  
    Scenario failed. You are in rails console becuase of @stop. Type exit when you are done
    ree-1.8.7-2010.02 :001 > p User.count
      SQL (0.2ms)  SELECT COUNT(*) FROM "users"
      1
    => nil
  

Running a single cucumber feature

If you want to execute only a single cucumber feature then the syntax for that is

  
    $ bundle exec cucumber features/products.feature --require features
  

Typing that long command could be tedious. This is what I do

  
    $ c features/products.feature
    $ bundle exec cucumber features/products.feature --require features
  

I typed the first line and the second line is what is actually executed. I have following lines in my ~/.bashrc.

  
    function c () {
      if [ $# -eq 0 ]; then
        local cmd="bundle exec cucumber"
      else
        local cmd="bundle exec cucumber $@ --require features"
      fi
      echo $cmd
      eval $cmd
    }
  
blog comments powered by Disqus