提交 81bc771e 编写于 作者: M Marcin Olichwirowicz

Remove mocha from ActionPack tests

上级 a8f4568f
......@@ -406,7 +406,6 @@ def jruby_skip(message = '')
skip message if defined?(JRUBY_VERSION)
end
require 'mocha/setup' # FIXME: stop using mocha
require 'active_support/testing/method_call_assertions'
class ForkingExecutor
......
......@@ -299,30 +299,42 @@ def setup
def test_output_buffer
output_buffer = ActionView::OutputBuffer.new
controller = MockController.new
cache_helper = Object.new
cache_helper = Class.new do
def self.controller; end;
def self.output_buffer; end;
def self.output_buffer=; end;
end
cache_helper.extend(ActionView::Helpers::CacheHelper)
cache_helper.expects(:controller).returns(controller).at_least(0)
cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
# if the output_buffer is changed, the new one should be html_safe and of the same type
cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
assert_nothing_raised do
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
cache_helper.stub :controller, controller do
cache_helper.stub :output_buffer, output_buffer do
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
assert_nothing_raised do
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
end
end
end
end
end
def test_safe_buffer
output_buffer = ActiveSupport::SafeBuffer.new
controller = MockController.new
cache_helper = Object.new
cache_helper = Class.new do
def self.controller; end;
def self.output_buffer; end;
def self.output_buffer=; end;
end
cache_helper.extend(ActionView::Helpers::CacheHelper)
cache_helper.expects(:controller).returns(controller).at_least(0)
cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
# if the output_buffer is changed, the new one should be html_safe and of the same type
cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
assert_nothing_raised do
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
cache_helper.stub :controller, controller do
cache_helper.stub :output_buffer, output_buffer do
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
assert_nothing_raised do
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
end
end
end
end
end
end
......
......@@ -28,8 +28,17 @@ def parse
end
end
class User; end
class Person; end
class User
def self.attribute_names
[]
end
end
class Person
def self.attribute_names
[]
end
end
tests UsersController
......@@ -155,33 +164,28 @@ def test_nested_params
end
def test_derived_wrapped_keys_from_matching_model
User.expects(:respond_to?).with(:attribute_names).returns(true)
User.expects(:attribute_names).twice.returns(["username"])
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, params: { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, params: { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }})
end
end
end
def test_derived_wrapped_keys_from_specified_model
with_default_wrapper_options do
Person.expects(:respond_to?).with(:attribute_names).returns(true)
Person.expects(:attribute_names).twice.returns(["username"])
assert_called(Person, :attribute_names, times: 2, returns: ["username"]) do
UsersController.wrap_parameters Person
UsersController.wrap_parameters Person
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, params: { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, params: { 'username' => 'sikachu', 'title' => 'Developer' }
assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }})
end
end
end
def test_not_wrapping_abstract_model
User.expects(:respond_to?).with(:attribute_names).returns(true)
User.expects(:attribute_names).returns([])
with_default_wrapper_options do
@request.env['CONTENT_TYPE'] = 'application/json'
post :parse, params: { 'username' => 'sikachu', 'title' => 'Developer' }
......
......@@ -379,7 +379,6 @@ def test_should_only_allow_cross_origin_js_get_without_xhr_header_if_protection_
end
def test_should_not_raise_error_if_token_is_not_a_string
@controller.unstub(:valid_authenticity_token?)
assert_blocked do
patch :index, params: { custom_authenticity_token: { foo: 'bar' } }
end
......
......@@ -246,12 +246,15 @@ def test_rescue_handler_string
end
def test_rescue_handler_with_argument
@controller.expects(:show_errors).once.with { |e| e.is_a?(Exception) }
get :record_invalid
assert_called_with @controller, :show_errors, [Exception] do
get :record_invalid
end
end
def test_rescue_handler_with_argument_as_string
@controller.expects(:show_errors).once.with { |e| e.is_a?(Exception) }
get :record_invalid_raise_as_string
assert_called_with @controller, :show_errors, [Exception] do
get :record_invalid_raise_as_string
end
end
def test_proc_rescue_handler
......
......@@ -272,9 +272,12 @@ def call(env)
test 'uses backtrace cleaner from env' do
@app = DevelopmentApp
cleaner = stub(:clean => ['passed backtrace cleaner'])
get "/", headers: { 'action_dispatch.show_exceptions' => true, 'action_dispatch.backtrace_cleaner' => cleaner }
assert_match(/passed backtrace cleaner/, body)
backtrace_cleaner = ActiveSupport::BacktraceCleaner.new
backtrace_cleaner.stub :clean, ['passed backtrace cleaner'] do
get "/", headers: { 'action_dispatch.show_exceptions' => true, 'action_dispatch.backtrace_cleaner' => backtrace_cleaner }
assert_match(/passed backtrace cleaner/, body)
end
end
test 'logs exception backtrace when all lines silenced' do
......
......@@ -25,27 +25,29 @@ def backtrace
exception = TestError.new("lib/file.rb:42:in `index'")
wrapper = ExceptionWrapper.new(nil, exception)
wrapper.expects(:source_fragment).with('lib/file.rb', 42).returns('foo')
assert_equal [ code: 'foo', line_number: 42 ], wrapper.source_extracts
assert_called_with(wrapper, :source_fragment, ['lib/file.rb', 42], returns: 'foo') do
assert_equal [ code: 'foo', line_number: 42 ], wrapper.source_extracts
end
end
test '#source_extracts works with Windows paths' do
exc = TestError.new("c:/path/to/rails/app/controller.rb:27:in 'index':")
wrapper = ExceptionWrapper.new(nil, exc)
wrapper.expects(:source_fragment).with('c:/path/to/rails/app/controller.rb', 27).returns('nothing')
assert_equal [ code: 'nothing', line_number: 27 ], wrapper.source_extracts
assert_called_with(wrapper, :source_fragment, ['c:/path/to/rails/app/controller.rb', 27], returns: 'nothing') do
assert_equal [ code: 'nothing', line_number: 27 ], wrapper.source_extracts
end
end
test '#source_extracts works with non standard backtrace' do
exc = TestError.new('invalid')
wrapper = ExceptionWrapper.new(nil, exc)
wrapper.expects(:source_fragment).with('invalid', 0).returns('nothing')
assert_equal [ code: 'nothing', line_number: 0 ], wrapper.source_extracts
assert_called_with(wrapper, :source_fragment, ['invalid', 0], returns: 'nothing') do
assert_equal [ code: 'nothing', line_number: 0 ], wrapper.source_extracts
end
end
test '#application_trace returns traces only from the application' do
......
......@@ -749,20 +749,23 @@ class RequestMethod < BaseRequestTest
class RequestFormat < BaseRequestTest
test "xml format" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => 'xml' })
assert_equal Mime::XML, request.format
assert_called(request, :parameters, times: 2, returns: {format: :xml}) do
assert_equal Mime::XML, request.format
end
end
test "xhtml format" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => 'xhtml' })
assert_equal Mime::HTML, request.format
assert_called(request, :parameters, times: 2, returns: {format: :xhtml}) do
assert_equal Mime::HTML, request.format
end
end
test "txt format" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => 'txt' })
assert_equal Mime::TEXT, request.format
assert_called(request, :parameters, times: 2, returns: {format: :txt}) do
assert_equal Mime::TEXT, request.format
end
end
test "XMLHttpRequest" do
......@@ -770,21 +773,25 @@ class RequestFormat < BaseRequestTest
'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest',
'HTTP_ACCEPT' => [Mime::JS, Mime::HTML, Mime::XML, "text/xml", Mime::ALL].join(",")
)
request.expects(:parameters).at_least_once.returns({})
assert request.xhr?
assert_equal Mime::JS, request.format
assert_called(request, :parameters, times: 1, returns: {}) do
assert request.xhr?
assert_equal Mime::JS, request.format
end
end
test "can override format with parameter negative" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :txt })
assert !request.format.xml?
assert_called(request, :parameters, times: 2, returns: {format: :txt}) do
assert !request.format.xml?
end
end
test "can override format with parameter positive" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :xml })
assert request.format.xml?
assert_called(request, :parameters, times: 2, returns: {format: :xml}) do
assert request.format.xml?
end
end
test "formats text/html with accept header" do
......@@ -810,23 +817,26 @@ class RequestFormat < BaseRequestTest
test "formats format:text with accept header" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :txt })
assert_equal [Mime::TEXT], request.formats
assert_called(request, :parameters, times: 2, returns: {format: :txt}) do
assert_equal [Mime::TEXT], request.formats
end
end
test "formats format:unknown with accept header" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ :format => :unknown })
assert_instance_of Mime::NullType, request.format
assert_called(request, :parameters, times: 2, returns: {format: :unknown}) do
assert_instance_of Mime::NullType, request.format
end
end
test "format is not nil with unknown format" do
request = stub_request
request.expects(:parameters).at_least_once.returns({ format: :hello })
assert request.format.nil?
assert_not request.format.html?
assert_not request.format.xml?
assert_not request.format.json?
assert_called(request, :parameters, times: 2, returns: {format: :hello}) do
assert request.format.nil?
assert_not request.format.html?
assert_not request.format.xml?
assert_not request.format.json?
end
end
test "format does not throw exceptions when malformed parameters" do
......@@ -837,8 +847,9 @@ class RequestFormat < BaseRequestTest
test "formats with xhr request" do
request = stub_request 'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
request.expects(:parameters).at_least_once.returns({})
assert_equal [Mime::JS], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [Mime::JS], request.formats
end
end
test "ignore_accept_header" do
......@@ -847,30 +858,37 @@ class RequestFormat < BaseRequestTest
begin
request = stub_request 'HTTP_ACCEPT' => 'application/xml'
request.expects(:parameters).at_least_once.returns({})
assert_equal [ Mime::HTML ], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats
end
request = stub_request 'HTTP_ACCEPT' => 'koz-asked/something-crazy'
request.expects(:parameters).at_least_once.returns({})
assert_equal [ Mime::HTML ], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats
end
request = stub_request 'HTTP_ACCEPT' => '*/*;q=0.1'
request.expects(:parameters).at_least_once.returns({})
assert_equal [ Mime::HTML ], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats
end
request = stub_request 'HTTP_ACCEPT' => 'application/jxw'
request.expects(:parameters).at_least_once.returns({})
assert_equal [ Mime::HTML ], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::HTML ], request.formats
end
request = stub_request 'HTTP_ACCEPT' => 'application/xml',
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
request.expects(:parameters).at_least_once.returns({})
assert_equal [ Mime::JS ], request.formats
assert_called(request, :parameters, times: 1, returns: {}) do
assert_equal [ Mime::JS ], request.formats
end
request = stub_request 'HTTP_ACCEPT' => 'application/xml',
'HTTP_X_REQUESTED_WITH' => "XMLHttpRequest"
request.expects(:parameters).at_least_once.returns({:format => :json})
assert_equal [ Mime::JSON ], request.formats
assert_called(request, :parameters, times: 2, returns: {format: :json}) do
assert_equal [ Mime::JSON ], request.formats
end
ensure
ActionDispatch::Request.ignore_accept_header = old_ignore_accept_header
end
......@@ -922,12 +940,14 @@ class RequestMimeType < BaseRequestTest
class RequestParameters < BaseRequestTest
test "parameters" do
request = stub_request
request.expects(:request_parameters).at_least_once.returns({ "foo" => 1 })
request.expects(:query_parameters).at_least_once.returns({ "bar" => 2 })
assert_equal({"foo" => 1, "bar" => 2}, request.parameters)
assert_equal({"foo" => 1}, request.request_parameters)
assert_equal({"bar" => 2}, request.query_parameters)
assert_called(request, :request_parameters, times: 2, returns: {"foo" => 1}) do
assert_called(request, :query_parameters, times: 2, returns: {"bar" => 2}) do
assert_equal({"foo" => 1, "bar" => 2}, request.parameters)
assert_equal({"foo" => 1}, request.request_parameters)
assert_equal({"bar" => 2}, request.query_parameters)
end
end
end
test "parameters not accessible after rack parse error" do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册