Skip to content

corntrace/rspec-isolation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running Your Test Cases in Isolation

The RSpec examples are generally run in a single ruby process. It dosen't matter until your code introduces some global variables, which would be polluted between examples. A common solution is to reset them in the after hook, but it breaks the DRY rule and would brings a lots of extra works. The best way is to run them in separated sub processes. This gem would help you to achieve this within a single method call.

Dependency

This gem depends on Rspec 2.0 and above. You should have it installed before using this gem.

Installation

sudo gem install rspec-isolation

Note: The installation would NOT automatically install the rspec 2.0.

Usage

Image a spec file exists:

$greeting = "Hello, world!"

describe "Test with global vars" do
  it "should be replaced with my name" do
    $greeting.gsub!(/\bworld\b/, "Kevin")
    $greeting.should == "Hello, Kevin!"
  end

  it "should be replaced with my hometown name" do
    $greeting.gsub!(/\bworld\b/, "Wuhan")
    $greeting.should == "Hello, Wuhan!"
  end
end

The $greeting is polluted in the first example and the second example would failed.

Now we require this gem:

require 'rspec/isolation'

Then you can choose one of the following ways to enable the isolation:

1. Using iso_it:

require 'rspec/isolation'
$greeting = "Hello, world!"

describe "Test with global vars" do
  iso_it "should be replaced with my name" do
    $greeting.gsub!(/\bworld\b/, "Kevin")
    $greeting.should == "Hello, Kevin!"
  end

  it "should be replaced with my hometown name" do
    $greeting.gsub!(/\bworld\b/, "Wuhan")
    $greeting.should == "Hello, Wuhan!"
  end
end

Now the first example would be run in a sub process.

2. Using run_in_isolation in before(:each) hook:

require 'rspec/isolation'
$greeting = "Hello, world!"

describe "Test with global vars" do
  before(:each) do
    run_in_isolation
  end
  it "should be replaced with my name" do
    $greeting.gsub!(/\bworld\b/, "Kevin")
    $greeting.should == "Hello, Kevin!"
  end

  it "should be replaced with my hometown name" do
    $greeting.gsub!(/\bworld\b/, "Wuhan")
    $greeting.should == "Hello, Wuhan!"
  end
end

Now all examples would be run in separated sub processes. Note: It won't work if you put run_in_isolation in a before(:all) hook.

Author

Kevin Fu, corntrace@gmail.com, @corntrace. If you like and use this gem, please feel free to give me any recommandation.

About

Make rspec examples run in separated processes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages