1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

More love for engines tests

This commit is contained in:
Piotr Sarnacki 2010-12-19 23:36:29 +01:00
parent 916c25aeee
commit d4f995301b

View file

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