Skip to content

Instantly share code, notes, and snippets.

@tcannonfodder
Created August 24, 2014 21:48
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 tcannonfodder/96fa736ec5c67cf43d1c to your computer and use it in GitHub Desktop.
Save tcannonfodder/96fa736ec5c67cf43d1c to your computer and use it in GitHub Desktop.
Deprecated Helpers when porting from test-spec to Minitest
# Deprecation warnings for common test-spec helpers, useful when porting tests
# to MiniTest by showing you what to replace it with.
# This was originally written by @madrobby for Freckle: https://letsfreckle.com
# remember to replace `YourApp` with the namespace of your app.
module YourApp::Assertions
mattr_accessor :deprecation_warnings
@@deprecation_warnings = []
class SpecResponder
def initialize(test_case)
@test_case = test_case
end
end
class Status < SpecResponder
def must_equal(status, message=nil)
@test_case.send(:assert_response, status, message)
end
def wont_equal(status, message=nil)
@test_case.send(:assert_response, status, message)
end
end
class Template < SpecResponder
def must_equal(template, message=nil)
@test_case.send(:assert_template, template, message)
end
end
class Layout < SpecResponder
def must_equal(layout, message=nil)
rendered_layout = @test_case.response.layout.gsub(/layouts\//, '')
@test_case.send(:assert_equal, layout, rendered_layout, message)
end
end
class Should < SpecResponder
def redirect_to(options = {}, message=nil)
@test_case.send(:assert_redirected_to, options, message)
end
def allow
Allow.new(@test_case)
end
def disallow
Disallow.new(@test_case)
end
def require_login
RequireLogin.new(@test_case)
end
end
class Allow < SpecResponder
def method_missing(verb, action, params={})
@test_case.send(verb, action, params)
assert !@test_case.send(:access_denied?)
end
end
class Disallow < SpecResponder
def method_missing(verb, action, params={})
@test_case.send(verb, action, params)
assert @test_case.send(:access_denied?)
end
end
class RequireLogin < SpecResponder
def method_missing(verb, action, params={})
@test_case.send(verb, action, params)
assert @test_case.send(:login_required?)
end
end
module DeprecatedActionControllerTestHelpers
attr_reader :request, :response
def deprecated(message)
YourApp::Assertions.deprecation_warnings << "#{message} in #{caller(2)[0]}"
end
def status
deprecated "'status' is deprecated, use assert_response"
YourApp::Assertions::Status.new(self)
end
def template
deprecated "'template' is deprecated, use assert_template"
YourApp::Assertions::Template.new(self)
end
def layout
deprecated "'template' is deprecated, use assert_layout"
YourApp::Assertions::Layout.new(self)
end
def should
deprecated "'should' is deprecated, use (TODO)"
YourApp::Assertions::Should.new(self)
end
end
end
# deprecation warnings for expectations
MiniTest::Expectations.instance_methods(false).each do |method|
Object.class_eval(<<-EOS
alias_method "original_#{method.to_s}".to_sym, "#{method.to_s}".to_sym
def #{method.to_s}(*args)
YourApp::Assertions.deprecation_warnings << "Don't use expectations (Object##{method.to_s} in \#{caller(1)[0]})"
original_#{method.to_s} *args
end
EOS
)
end
# deprecation warnings for ActionController test extensions
ActionController::TestCase.send(:include, YourApp::Assertions::DeprecatedActionControllerTestHelpers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment