Search code examples
rubyrspecguard

Ruby: Specs pass when run directly, TypeError when run in Guard


I am updating a gem to make sure it works with the new versions of the bitly and rspec gems, and I have run into an odd issue.

When I run guard to make sure all the tests pass with the new gem, I get a TypeError; however, if I run my test suite directly with just rspec they all pass and do not throw the error.

My code is available on GitHub if you want to see the whole shebang.

From digging around, I've seen people suggesting to wrap the class inside of its own module to keep the class names from conflicting, but since it works fine with just rspec, I am hoping I don't need to add another layer.

Here's the TypeError:

/Users/jstim/Documents/Programming/Ruby/uncoil/lib/uncoil.rb:6:in '': Uncoil is not a class (TypeError)
from /Users/jstim/Documents/Programming/Ruby/uncoil/spec/uncoil_spec.rb:1:in 'require_relative'
from /Users/jstim/Documents/Programming/Ruby/uncoil/spec/uncoil_spec.rb:1:in ''
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in 'load'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in 'block in load_spec_files'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in 'map'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/configuration.rb:698:in 'load_spec_files'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/command_line.rb:22:in 'run'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:80:in 'run_in_process'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:69:in 'run'
from /Users/jstim/.rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.8.0/lib/rspec/core/runner.rb:10:in 'block in autorun'

Let me know if I can include additional code to help out. thanks!


Solution

  • The problem is that in "lib/uncoil", you define class Uncoil but in "lib/uncoil/version" you define module Uncoil. If the version gets loaded, there will be a conflict in that you're trying to reopen a class that is actually a module (or vice versa, depending on which gets loaded first).

    I assume that when you run rspec spec that it doesn't use bundler at all (potential problem, btw, as your dependencies aren't being managed, so you should always do bundle exec rspec spec), so it never loads the .gemspec, which is the only place you require the version file. Presumably Bundler loads the gemspecs, which in turn loads version file, causing the conflict. If you didn't experience this before, I would guess that one of your dependencies (probably guard-rspec) was changed to load Bundler in the newer version.

    You might consider specifying versions on the dependencies in the gemspec.