Skip to content

Instantly share code, notes, and snippets.

@peter
Created May 28, 2010 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/416978 to your computer and use it in GitHub Desktop.
Save peter/416978 to your computer and use it in GitHub Desktop.
# Notes on migrating from the RSpec builtin mocking framework to the Mocha framework
# 1. Comment in this line in spec_helper.rb:
config.mock_with :mocha
# 2. Specify your gem dependency (optional) in config/environments/test.rb:
config.gem "mocha", :version => "0.9.8"
# 3. Use query-replace to do most but not all of the DSL conversion
# should_receive -> expects
find spec -type f -iname '*.rb'|xargs ruby -pi.bak -e 'gsub(/\.should_receive\(/, ".expects(")'
# stub! -> stubs
find spec -type f -iname '*.rb'|xargs ruby -pi.bak -e 'gsub(/\.stub!\(/, ".stubs(")'
# and_return -> returns
find spec -type f -iname '*.rb'|xargs ruby -pi.bak -e 'gsub(/\.and_return\(/, ".returns(")'
# and_raise -> raises
find spec -type f -iname '*.rb'|xargs ruby -pi.bak -e 'gsub(/\.and_raise\(/, ".raises(")'
# mock(:symbol) -> mock(string)
find spec -type f -iname '*.rb'|xargs ruby -pi.bak -e "gsub(/mock\(:([a-z0-9_]+)\)/, 'mock(\'\1\')')"
# 4. Inspect and tweak the query-replace changes if needed
# 5. Do the following changes manually (or with a smart query-replace):
# - should_return with block becomes .with(arg_value1).returns(return_value1)
# - should_not_receive => expects.never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment