Skip to content

Instantly share code, notes, and snippets.

@kristjan
Created October 14, 2015 05:05
Show Gist options
  • Save kristjan/0c242c3d2ee19e393d61 to your computer and use it in GitHub Desktop.
Save kristjan/0c242c3d2ee19e393d61 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Automatically convert most of the syntax differences between Mocha and RSpec
# Mocks.
#
# Does not handle:
# - Multiline statements like `Foo\n.expects`
# - `with` being given a block
# - Probably other things
#
# You should also check your new uses of `double` for places you could use
# `instance_double` to get protection against stubbing nonexistant methods.
files = Dir[File.join('spec', '**', '*')].select {|file| File.file?(file)}
CONVERSIONS = {
/(?<=\b)(\S*)\.stubs(?=\()/ => 'allow(\\1).to receive',
/(?<=\b)(\S*)\.expects(?=\()/ => 'expect(\\1).to receive',
/(?<=\.)returns(?=\()/ => 'and_return',
/(?<=\.)raises(?=\()/ => 'and_raise',
/(?<=\b)stub(?=\()/ => 'double'
}
files.each do |file|
contents = File.read(file)
CONVERSIONS.each do |regexp, replacement|
contents.gsub!(regexp, replacement)
end
File.open(file, 'w') { |f| f.puts contents }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment