Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix bug when Rails.application is defined but is nil. See #881 #6429

Merged
merged 1 commit into from

3 participants

@marcandre

Got hit by this bug today. Credits should go to Josep M. Bach josep.m.bach@gmail.com, see #881.

@carlosantoniodasilva

I remember having this issue while working on rails-api with @spastorino, the solution iirc was to actually define an application (ie inherit from Rails::Application somewhere in our test_helper).

@marcandre do you have a small example that shows the issue? I don't remember exactly how it happened at that time. Thanks!

@josevalim
Owner

it will probably happen in any gem that defines a railtie and uses integration testing.

@marcandre

It's possible I wasn't doing the right thing, but I needed an ActiveRecord based model in one test, so I created one (https://github.com/marcandre/table_helper/blob/master/test/unit/model_test.rb ) and that had impact on another test (https://github.com/marcandre/table_helper/blob/master/test/helpers/table_helper_test.rb ) because now the TestCase could not be instantiated anymore.

Maybe there is something else that could be fixed, but I feel the patch should be accepted. It's very possible that defining an application works, but it shoudn't be mandatory if it can be avoided.

@carlosantoniodasilva

Agreed, I just wanted to remember what was the use case, thanks for clarifying, I think mine at that time was a bit different, but I've got the same error. Anyway, the patch looks fine, thank you.

@carlosantoniodasilva carlosantoniodasilva merged commit c5fc159 into from
@derekcroft derekcroft referenced this pull request from a commit in coupa/delocalize
@clemens clemens Don't run tests in Rails 3.0/3.1 at the moment
Reason: rails/rails#6429 seems to not have been backported for 3.0/3.1.
f6305ef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  actionpack/lib/action_dispatch/testing/test_request.rb
@@ -11,7 +11,7 @@ def self.new(env = {})
end
def initialize(env = {})
- env = Rails.application.env_config.merge(env) if defined?(Rails.application)
+ env = Rails.application.env_config.merge(env) if defined?(Rails.application) && Rails.application
super(DEFAULT_ENV.merge(env))
self.host = 'test.host'
View
7 actionpack/test/dispatch/test_request_test.rb
@@ -55,6 +55,13 @@ class TestRequestTest < ActiveSupport::TestCase
assert_cookies({"user_name" => "david"}, req.cookie_jar)
end
+ test "does not complain when Rails.application is nil" do
+ Rails.stubs(:application).returns(nil)
+ req = ActionDispatch::TestRequest.new
+
+ assert_equal false, req.env.empty?
+ end
+
private
def assert_cookies(expected, cookie_jar)
assert_equal(expected, cookie_jar.instance_variable_get("@cookies"))
Something went wrong with that request. Please try again.