From d4f995301bace57070e37ac97b9b18a70aed2230 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Sun, 19 Dec 2010 23:36:29 +0100 Subject: [PATCH] More love for engines tests --- railties/test/railties/engine_test.rb | 126 ++++++++++++-------------- 1 file changed, 57 insertions(+), 69 deletions(-) diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 8a003228b7..92aa025238 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1,12 +1,15 @@ require "isolation/abstract_unit" require "railties/shared_tests" require 'stringio' +require 'rack/test' +require 'rack/file' module RailtiesTest class EngineTest < Test::Unit::TestCase include ActiveSupport::Testing::Isolation include SharedTests + include Rack::Test::Methods def setup build_app @@ -87,10 +90,8 @@ module RailtiesTest boot_rails - env = Rack::MockRequest.env_for("/bukkits") - response = Rails.application.call(env) - - assert_body "HELLO WORLD", response + get("/bukkits") + assert_equal "HELLO WORLD", last_response.body end test "it provides routes as default endpoint" do @@ -115,9 +116,8 @@ module RailtiesTest boot_rails - env = Rack::MockRequest.env_for("/bukkits/foo") - response = Rails.application.call(env) - assert_body "foo", response + get("/bukkits/foo") + assert_equal "foo", last_response.body end test "engine can load its own plugins" do @@ -191,13 +191,11 @@ module RailtiesTest boot_rails env = Rack::MockRequest.env_for("/") - response = Bukkits::Engine.call(env) - + Bukkits::Engine.call(env) assert_equal Bukkits::Engine.routes, env['action_dispatch.routes'] env = Rack::MockRequest.env_for("/") - response = Rails.application.call(env) - + Rails.application.call(env) assert_equal Rails.application.routes, env['action_dispatch.routes'] end @@ -230,6 +228,12 @@ module RailtiesTest <%= stylesheet_link_tag("foo") %> ERB + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + mount Bukkits::Engine => "/bukkits" + end + RUBY + add_to_config 'config.asset_path = "/omg%s"' boot_rails @@ -239,9 +243,8 @@ module RailtiesTest ::Bukkits::Engine.config.asset_path = "/bukkits%s" - env = Rack::MockRequest.env_for("/foo") - response = Bukkits::Engine.call(env) - stripped_body = response[2].body.split("\n").map(&:strip).join + get("/bukkits/foo") + stripped_body = last_response.body.split("\n").map(&:strip).join expected = "/omg/bukkits/images/foo.png" + "" + @@ -264,14 +267,16 @@ module RailtiesTest end RUBY + app_file "config/routes.rb", <<-RUBY + AppTemplate::Application.routes.draw do + mount Bukkits::Engine => "/bukkits" + end + RUBY + boot_rails - env = Rack::MockRequest.env_for("/foo") - response = Bukkits::Engine.call(env) - stripped_body = response[2].body.strip - - expected = "/bukkits/images/foo.png" - assert_equal expected, stripped_body + get("/bukkits/foo") + assert_equal "/bukkits/images/foo.png", last_response.body.strip end test "engine's files are served via ActionDispatch::Static" do @@ -291,17 +296,14 @@ module RailtiesTest boot_rails - env = Rack::MockRequest.env_for("/app.html") - response = Rails.application.call(env) - assert_body File.read(File.join(app_path, "public/app.html")), response + get("/app.html") + assert_equal File.read(File.join(app_path, "public/app.html")), last_response.body - env = Rack::MockRequest.env_for("/bukkits/bukkits.html") - response = Rails.application.call(env) - assert_body File.read(File.join(@plugin.path, "public/bukkits.html")), response + get("/bukkits/bukkits.html") + assert_equal File.read(File.join(@plugin.path, "public/bukkits.html")), last_response.body - env = Rack::MockRequest.env_for("/bukkits/file_from_app.html") - response = Rails.application.call(env) - assert_body File.read(File.join(app_path, "public/bukkits/file_from_app.html")), response + get("/bukkits/file_from_app.html") + assert_equal File.read(File.join(app_path, "public/bukkits/file_from_app.html")), last_response.body end test "shared engine should include application's helpers and own helpers" do @@ -347,20 +349,14 @@ module RailtiesTest boot_rails - env = Rack::MockRequest.env_for("/foo") - response = Rails.application.call(env) - assert_body "Something... Something... Something...", response - response[2].close + get("/foo") + assert_equal "Something... Something... Something...", last_response.body - env = Rack::MockRequest.env_for("/foo/show") - response = Rails.application.call(env) - assert_body "/foo", response - response[2].close + get("/foo/show") + assert_equal "/foo", last_response.body - env = Rack::MockRequest.env_for("/foo/bar") - response = Rails.application.call(env) - assert_body "It's a bar.", response - response[2].close + get("/foo/bar") + assert_equal "It's a bar.", last_response.body end test "isolated engine should include only its own routes and helpers" do @@ -459,30 +455,20 @@ module RailtiesTest assert ::Bukkits::MyMailer.method_defined?(:foo_path) assert !::Bukkits::MyMailer.method_defined?(:bar_path) - env = Rack::MockRequest.env_for("/bukkits/from_app") - response = AppTemplate::Application.call(env) - assert_body "false", response - response[2].close + get("/bukkits/from_app") + assert_equal "false", last_response.body - env = Rack::MockRequest.env_for("/bukkits/foo/show") - response = AppTemplate::Application.call(env) - assert_body "/bukkits/foo", response - response[2].close + get("/bukkits/foo/show") + assert_equal "/bukkits/foo", last_response.body - env = Rack::MockRequest.env_for("/bukkits/foo") - response = AppTemplate::Application.call(env) - assert_body "Helped.", response - response[2].close + get("/bukkits/foo") + assert_equal "Helped.", last_response.body - env = Rack::MockRequest.env_for("/bukkits/routes_helpers_in_view") - response = AppTemplate::Application.call(env) - assert_body "/bukkits/foo, /bar", response - response[2].close + get("/bukkits/routes_helpers_in_view") + assert_equal "/bukkits/foo, /bar", last_response.body - env = Rack::MockRequest.env_for("/bukkits/polymorphic_path_without_namespace") - response = AppTemplate::Application.call(env) - assert_body "/bukkits/posts/1", response - response[2].close + get("/bukkits/polymorphic_path_without_namespace") + assert_equal "/bukkits/posts/1", last_response.body end test "isolated engine should avoid namespace in names if that's possible" do @@ -541,9 +527,8 @@ module RailtiesTest boot_rails - env = Rack::MockRequest.env_for("/bukkits/posts/new") - response = AppTemplate::Application.call(env) - assert extract_body(response) =~ /name="post\[title\]"/ + get("/bukkits/posts/new") + assert_match /name="post\[title\]"/, last_response.body end test "loading seed data" do @@ -612,16 +597,14 @@ module RailtiesTest end RUBY - require 'rack/test' - extend Rack::Test::Methods - boot_rails require "#{rails_root}/config/environment" - get "/foo" + + get("/foo") assert_equal "foo", last_response.body - get "/bukkits/bar" + get("/bukkits/bar") assert_equal "bar", last_response.body end @@ -785,5 +768,10 @@ module RailtiesTest assert_match %r{bukkits/stylesheets/foo.css}, last_response.body assert_match %r{bukkits/javascripts/foo.js}, last_response.body end + + private + def app + Rails.application + end end end