pat / combustion

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

uninitialized constant Rails (NameError) #70

Closed bobf closed 8 years ago

bobf commented 8 years ago

I really hope this isn't a duplicate of #27 - apologies if so !

 $ ruby --version
ruby 2.1.6p336 (2015-04-13 revision 50298) [x86_64-darwin13.0]
 $ rails --version
Rails 4.2.5
 $ rails plugin new combustable -T --mountable
      create
      create  README.rdoc
      create  Rakefile
      create  combustable.gemspec
      create  MIT-LICENSE
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/combustable/application_controller.rb
      create  app/helpers/combustable/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/combustable/application.html.erb
      create  app/assets/images/combustable
      create  app/assets/images/combustable/.keep
      create  config/routes.rb
      create  lib/combustable.rb
      create  lib/tasks/combustable_tasks.rake
      create  lib/combustable/version.rb
      create  lib/combustable/engine.rb
      create  app/assets/stylesheets/combustable/application.css
      create  app/assets/javascripts/combustable/application.js
      create  bin
      create  bin/rails
         run  bundle install
 $ cd combustable
# [ edit Gemfile ]
 $ bundle install
Resolving dependencies...
Using rake 10.5.0
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.3
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.5
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile2 2.0.0
Using nokogiri 1.6.7.2
Using rails-deprecated_sanitizer 1.0.3
Using rails-dom-testing 1.0.7
Using loofah 2.0.3
Using rails-html-sanitizer 1.0.2
Using actionview 4.2.5
Using rack 1.6.4
Using rack-test 0.6.3
Using actionpack 4.2.5
Using globalid 0.3.6
Using activejob 4.2.5
Using mime-types 2.99
Using mail 2.6.3
Using actionmailer 4.2.5
Using activemodel 4.2.5
Using arel 6.0.3
Using activerecord 4.2.5
Using bundler 1.10.0.rc
Using xpath 2.0.0
Using capybara 2.5.0
Using thor 0.19.1
Using railties 4.2.5
Using concurrent-ruby 1.0.0
Using sprockets 3.5.2
Using sprockets-rails 3.0.0
Using rails 4.2.5
Using combustable 0.0.1 from source at .
Using combustion 0.5.4
Using diff-lcs 1.2.5
Using rspec-support 3.3.0
Using rspec-core 3.3.2
Using rspec-expectations 3.3.1
Using rspec-mocks 3.3.2
Using rspec-rails 3.3.3
Using sqlite3 1.3.11
 $ bin/rails g rspec:install
      create  .rspec
      create  spec
      create  spec/spec_helper.rb
      create  spec/rails_helper.rb
# [ edit spec/spec_helper.rb - see below ]

 $ combust
      create  spec/internal
      create  spec/internal/config
      create  spec/internal/db
      create  spec/internal/log
      create  spec/internal/public
      create  spec/internal/config/routes.rb
      create  spec/internal/config/database.yml
      create  spec/internal/db/schema.rb
      create  config.ru
      create  spec/internal/public/favicon.ico
      create  spec/internal/log/.gitignore
 $ rackup
/Users/bobfarrell/dev/combustable/lib/combustable/engine.rb:2:in `<module:Combustable>': uninitialized constant Rails (NameError)
    from /Users/bobfarrell/dev/combustable/lib/combustable/engine.rb:1:in `<top (required)>'
    from /Users/bobfarrell/dev/combustable/lib/combustable.rb:1:in `require'
    from /Users/bobfarrell/dev/combustable/lib/combustable.rb:1:in `<top (required)>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:76:in `require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:72:in `each'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:72:in `block in require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:61:in `each'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:61:in `require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler.rb:133:in `require'
    from /Users/bobfarrell/dev/combustable/config.ru:4:in `block in <main>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /Users/bobfarrell/dev/combustable/config.ru:in `new'
    from /Users/bobfarrell/dev/combustable/config.ru:in `<main>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:272:in `start'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:147:in `start'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/bin/rackup:4:in `<top (required)>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/bin/rackup:23:in `load'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/bin/rackup:23:in `<main>'

Gemfile:

source 'https://rubygems.org'

# Declare your gem's dependencies in combustable.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]

group :test do
  gem 'combustion', '~> 0.5.4'
  gem 'rspec-rails'
  gem 'capybara'
end

spec/spec_helper.rb (I guess this isn't relevant ?)

require 'rubygems'
require 'bundler/setup'

require 'combustion'
require 'capybara/rspec'

Combustion.initialize! :all

require 'rspec/rails'
require 'capybara/rails'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
  # These two settings work together to allow you to limit a spec run
  # to individual examples or groups you care about by tagging them with
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
  # get run.
  config.filter_run :focus
  config.run_all_when_everything_filtered = true

  # Allows RSpec to persist some state between runs in order to support
  # the `--only-failures` and `--next-failure` CLI options. We recommend
  # you configure your source control system to ignore this file.
  config.example_status_persistence_file_path = "spec/examples.txt"

  # Limits the available syntax to the non-monkey patched syntax that is
  # recommended. For more details, see:
  #   - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
  #   - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
  #   - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
  config.disable_monkey_patching!

  # Many RSpec users commonly either run the entire suite or an individual
  # file, and it's useful to allow more verbose output when running an
  # individual spec file.
  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = 'doc'
  end

  # Print the 10 slowest examples and example groups at the
  # end of the spec run, to help surface which specs are running
  # particularly slow.
  config.profile_examples = 10

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = :random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed
=end
end

Let me know if you need any more info. Thanks !

pat commented 8 years ago

Greatly appreciate all the detail!

I think this is actually a flaw in the plugin generator - I'd expect a gem that relies on Rails::Engine to have require 'rails/engine', instead of just hoping something else along the way will load it. Open to other thoughts on the matter though :)

bobf commented 8 years ago

By the way, the initial reason for creating this issue was that I couldn't get rspec to run (error about needing a rack-test application present) but, in putting together this recipe, I noticed that if I copy more of the sample spec_helper.rb than I thought I needed, my specs now run (I have a passing test visiting a page inside my internal app). So that's the most important bit for me - rackup would just be a bonus.

Perhaps the docs could be a bit more specific about exactly what and why needs to go into spec_helper.rb ?

pat commented 8 years ago

I've not used the rspec-rails generator from within an engine - I'm not sure it was built for that purpose? The spec_helper.rb in the README is what I expect should work - if you're able to reproduce your original rack-test error, that'd be super helpful.

bobf commented 8 years ago

Interesting ! Adding require 'rails/engine' to my engine.rb gives a new error:

$ rackup
/Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/engine.rb:531:in `routes': uninitialized constant Rails::Engine::ActionDispatch (NameError)
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/railtie.rb:194:in `public_send'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/railtie.rb:194:in `method_missing'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/railties-4.2.5/lib/rails/engine.rb:380:in `isolate_namespace'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/lib/hierarchy_navigator/engine.rb:5:in `<class:Engine>'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/lib/hierarchy_navigator/engine.rb:4:in `<module:HierarchyNavigator>'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/lib/hierarchy_navigator/engine.rb:3:in `<top (required)>'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/lib/hierarchy_navigator.rb:1:in `require'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/lib/hierarchy_navigator.rb:1:in `<top (required)>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:76:in `require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:72:in `each'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:72:in `block in require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:61:in `each'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler/runtime.rb:61:in `require'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/bundler-1.10.0.rc/lib/bundler.rb:133:in `require'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/config.ru:4:in `block in <main>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/config.ru:in `new'
    from /Users/bobfarrell/dev/ph/hierarchy_navigator/config.ru:in `<main>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:272:in `start'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:147:in `start'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.6.4/bin/rackup:4:in `<top (required)>'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/bin/rackup:23:in `load'
    from /Users/bobfarrell/.rbenv/versions/2.1.6/bin/rackup:23:in `<main>'

Sorry if there is an obvious solution to this, I'm reasonably new to Rails internals, especially with engines.

bobf commented 8 years ago

Sure thing. With this spec/spec_helper.rb:

require 'combustion'

Combustion.initialize! :all

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

  config.order = :random

  Kernel.srand config.seed
end

I get this error when running rspec

     ArgumentError:
       rack-test requires a rack application, but none was given

It seems obvious now that adding these two lines after Combustion.initalize! :all would solve the problem, but it was difficult from the example you provided to figure out what was expected to be in a "standard" spec_helper.rb and what was expected to be added by the user:

require 'rspec/rails'
require 'capybara/rails'

Maybe a barebones spec_helper.rb would be better, i.e. the minimum that it must contain in order for Combustion to work ?

bobf commented 8 years ago

The point being above that it seemed odd to me to need to require rspec/rails for this to work, given that the rspec:install generator does not include them, yet rspec itself works fine. Does that make sense ?

bobf commented 8 years ago

Hey, bundle exec rackup works for me now ! No idea what the hell I did to make it work. :)

Anyway, thanks for the help earlier and for the great gem. Took a while to get off the ground but will definitely be using this for future engine testing now I know how to get it going.

Cheers,

pat commented 8 years ago

Great to know you got it working - I'll look into improving the documentation for the spec_helper.rb example :)

NullVoxPopuli commented 7 years ago

Anybody have an app I can look at? I'm getting the same error @bobf was getting about ActionDispatch. I'm just trying to run tests.

pat commented 7 years ago

@NullVoxPopuli I don't suppose the source of the engine/railtie you're working on is visible on GitHub?

I can point to a couple of gems of my own that use Combustion, if that's helpful, though I generally don't make use of the controllers/views/routing in my engines: