pat / combustion

Simple, elegant testing for Rails Engines
MIT License
708 stars 51 forks source link

Mounting the engine gives errors, adding manual route works. #62

Closed fwl closed 9 years ago

fwl commented 9 years ago

Mounting the engine in spec/internal/config/routes.rb gives routing errors (route not found etc.):

  mount Foo::Engine, at: "/foo"

While manually adding a route to the engine works fine and all tests pass:

  post "/foo/widgets", to: "foo/widgets#create"

Any ideas why this happens? Could it be a bug or is it an oversight on my behalf?

Thank you!

pat commented 9 years ago

@fwl this certainly sounds like something hasn't been configured quite correctly. Have you defined routes for your engine as well?

fwl commented 9 years ago

Hi @pat and thank you for your response.

Yes, I have the following:

# config/routes.rb

MyEngine::Engine.routes.draw do
  resources :widgets, only: [ :create ], constraints: { format: "json" }
end

When the engine is used in a real app, everything works fine. This problem only occurs in tests.

pat commented 9 years ago

Huh, that is odd indeed. Which versions of Combustion and Rails are you using?

fwl commented 9 years ago

This is what I have in the gemspec:

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.1"
spec.add_development_dependency "combustion", "~> 0.5.2"
spec.add_development_dependency "rspec-rails", "~> 3.1"
spec.add_development_dependency "capybara", "~> 2.4.4"
spec.add_development_dependency "ammeter", "~> 1.1.2"
spec.add_development_dependency "webmock", "~> 1.20"

spec.add_dependency "rails", ">= 3.2"
spec.add_dependency "jquery-rails"

and this is what I have in the Gemfile:

source 'https://rubygems.org'

gemspec

group :test do
  gem "actionpack"
  gem "sprockets"
  gem "jquery-rails"
  gem "poltergeist", github: 'teampoltergeist/poltergeist'
end
fwl commented 9 years ago
# Gemfile.lock
# ...
combustion (0.5.2)
rails (4.2.0)
# ...
pat commented 9 years ago

And your app that you're using it in is Rails 4.2 as well?

Can you try dropping down to Combustion 0.5.1, see if that helps?

fwl commented 9 years ago

I just tried Combustion 0.5.1 and the test fails just like with 0.5.2 (i.e. if I use mount Foo::Engine, at: "/foo" as opposed to post "/foo/widgets", to: "foo/widgets#create").

Regarding your question about the app where I am using the engine, if you mean the real-world app where I use the engine, yes, it is a Rails 4.2 app. The tests fail with RSpec and the spec/internal app that Combustion generated.

pat commented 9 years ago

I wonder if this is related to Rails 4.2? Is it possible for you to lock Rails to a 4.1 release in your engine's gemspec, bundle update, and see how that goes?

Otherwise, I don't suppose the code for this engine is available publicly on GitHub or elsewhere?

fwl commented 9 years ago

Hi @pat,

Locking to Rails 4.1 produces the same error when running the tests...

The code isn't publicly available currently but I could paste parts of it if it'd help. I'm just struggling to see what could be problematic though, since the engine works fine in production. The issue appears only when running tests with Combustion and RSpec.

pat commented 9 years ago

Hmm. Can you share your spec_helper?

fwl commented 9 years ago

Hi @pat,

Certainly. It is:

#spec/spec_helper.rb

require "combustion"
require "name_of_my_gem"

Combustion.initialize! :action_controller, :sprockets

require "rspec/rails"
require "capybara/rails"

require "webmock/rspec"

WebMock.disable_net_connect! allow_localhost: true

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  if config.files_to_run.one?
    config.default_formatter = "doc"
  end

  config.order = :random

  Kernel.srand config.seed
end
fwl commented 9 years ago

Hi @pat,

A quick update. I set up appraisal to test with Rails 3.2, 4.0, 4.1, and 4.2. While doing that I hit https://github.com/pat/combustion/issues/50 so I had to make changes to spec_helper.rb to get it to work. The new spec_helper.rb is below. Despite these changes the issue we're discussing persists and the specs fail in all versions of Rails that I am testing (>= 3.2).

#spec/spec_helper.rb

require "rails"
require "action_controller/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require "jquery/rails"
require "combustion"
require "name_of_my_gem"

Combustion.initialize! :action_controller, :sprockets do
  config.log_level = :warn
end

require "rspec/rails"
require "capybara/rails"

require "webmock/rspec"

WebMock.disable_net_connect! allow_localhost: true

RSpec.configure do |config|
  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  if config.files_to_run.one?
    config.default_formatter = "doc"
  end

  config.order = :random

  Kernel.srand config.seed
end
pat commented 9 years ago

If it's a private project on GitHub, I don't suppose it's possible for you to add me so I can debug directly? Completely understand if you'd rather not - I'm just really not sure what the problem is :|

fwl commented 9 years ago

Hi @pat,

Sorry for the delayed response, I was offline for several days.

Thank you for offering to debug the gem, it is very kind of you. However, it feels to me that this would be asking too much from a library author. I'll continue investigating this by myself for now and if any new findings come to surface I will let you know.

Once again, thank you for writing this in the first place and for being so helpful.

fwl commented 9 years ago

Hi @pat,

The issue I was having is fixed now (after a lot of painful debugging) and I'm happy to report back that it was not related to Combustion, but to some rather esoteric Rails engine routing/helpers issue.

Please feel free to close this issue and thank you once again for trying to help with this.

pat commented 9 years ago

Ah, shame it was so painful to find, but great to know you got to the bottom of it!