Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Trouble testing engine when using gemspec inside Gemfile #1442
bundle -v = 1.0.18
Gem::Specification.new do |s|
Make sure to explicitly set require in engine.rb
can't rely on bundler require
Make sure to explicitly set require in engine.rb or spec_helper.rb
can't rely on bundler require
dependencies that do not seem to fit the gemspec mold
gem 'rake', '~> 0.8.7'
Call the gemspec
override gemspec that require different names
gem 'ruby-debug19', :require => 'ruby-debug'
Misc lines in my application.rb
Bundler.require(:default, Rails.env) if defined?(Bundler)
I understand that the two lines above are redundant, but that said it seemed to do something.
So I have an engine in a rails 3.0 app, actually I have three. The main app itself is sort of a controller for the three engines which themselves are separate apps, it does the stuff that is duplicated across all servers such as layouts and some models. All testing is done in each individual app as an attempt to silo each. Since it is an engine it is required to have a gemspec so the main app knows what it needs when it calls that engine, so to make it more dry I have a Gemfile that calls my gemspec so it can be stand-alone and all of my gems are defined in the gemspec.
My problem is when I do testing, it complains about uninitialized constants from many of the gems that were supposed to be loaded. For example, I can run my specs and every single one fails because DatabaseCleaner is an uninitialized constant. I can fix this by adding the exact same gem to my Gemfile by itself or in a :development group. I can also fix this by adding it as a require 'database_cleaner' somewhere in my application, either application.rb or spec_helper.rb. After I've done this, more tests fail because it can't see Typhoeus, so I'll do one of the two fixes I've outlined then something else will fail for another reason and so on. Eventually, all my tests will pass after I've hacked my code sufficiently.
My question is, why is defining something in a Gemfile different from defining in a gemspec and just including that gemspec reference in the Gemfile? More importantly, am I assuming correctly that Bundler.require() simply sees the installed gems and put a "require" in front of all these gems? I've tried looking all over the internet for a solution and thus far I've found in general people say "Don't trust the Bundler.require() to include all necessary modules." I feel like there needs to be a more elegant solution than maintaining a Gemfile and a gemspec that have the same data or making sure to require all necessary modules individually somewhere in the application, which seems wonky for other reasons.
I know this may look like a repeat, but the answers to those questions other have given haven't been able to sufficiently answer my own questions.
Any help is greatly appreciated, thank you.
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
After thinking about it for awhile, this current behavior is what I would expect.
When using a gemspec,
When developing an app, this is what I want, as I would put all the gems my app explicitly depends on in the Gemfile, and let the gems resolve their own dependencies when they are required.
The problem comes about when developing something that feels like an app (a Rails Engine), but is packaged as a gem. I cannot depend on the host Rails app to require my engine's dependencies for me, because I am a gem. So I must require my dependencies explicitly (somewhere like lib/foo.rb), and not use Bundler.require.
OK, yep, this does makes sense. I forgot that Bundler would still assist with the load path, so that makes it a lot easier to understand.
It still doesn't feel very DRY to list my gems in the
Hold on... Bundler doesn't seem to assist with the load paths. 0337810 does seem to make Bundler completely ignorant of all runtime dependencies in the gemspec.
But if I add the runtime dependencies manually in the Gemfile, it runs fine: