Ruby:规范直接运行时通过,在保护状态下运行时输入错误

时间:2022-09-01 21:21:46

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.

我正在更新一个gem,以确保它与bitly和rspec gems的新版本兼容,我遇到了一个奇怪的问题。

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.

当我运行guard时,确保所有测试都通过了新的gem,我得到了一个TypeError;但是,如果我直接使用rspec运行测试套件,它们都通过了,不会抛出错误。

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

我的代码可以在GitHub上找到,如果你想看完整的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.

通过深入研究,我看到有人建议将类封装到它自己的模块中,以避免类名之间的冲突,但是由于它只适用于rspec,所以我希望我不需要添加另一个层。

Here's the TypeError:

这是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'

/用户/ jstim /文件/程序/ Ruby /展开/ lib /展开。Uncoil不是一个类(类型错误)来自/用户/jstim/文档/编程/Ruby/ Uncoil /spec/uncoil_spec。rb:1:' require_relative '从/用户/ jstim /文档/编程/ Ruby /展开/规范/ uncoil_spec。rb:1:“从/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/配置。rb:698:“负载”/用户/ jstim / .rvm /宝石/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/配置。rb:698:来自/用户/jstim/.rvm/gems/ruby-1.9.2-p290/gems/ gems/rspec-core-2.8.0/lib/rspec/core/configuration。rb:698:在“地图”/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/配置。rb:698:“load_spec_files”从/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/ command_line。rb:22:在“运行”/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/跑步者。rb:80:“run_in_process”从/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/跑步者。rb:69:在“运行”/用户/ jstim / .rvm /珠宝/ ruby-1.9.2-p290 /珠宝/ rspec-core-2.8.0 / lib / rspec /核心/跑步者。rb:10:在自动运行的

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

请告诉我是否可以包含其他代码来帮助您。谢谢!

1 个解决方案

#1


6  

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).

问题是,在“lib/uncoil”中,您定义了类un线圈,而在“lib/uncoil/version”中,您定义了模块uncoil。如果该版本被加载,则会出现冲突,因为您试图重新打开一个实际上是模块的类(反之亦然,这取决于首先加载哪个类)。

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.

我假设当您运行rspec时,它根本不使用bundler(顺便说一句,由于您的依赖项没有被管理,所以您应该始终执行bundle exec rspec),因此它不会加载.gemspec,这是您需要版本文件的惟一位置。据推测,Bundler将加载gemspecs,进而加载版本文件,从而导致冲突。如果您以前没有经历过这种情况,我猜想您的一个依赖项(可能是guard-rspec)在更新的版本中被更改为装入Bundler。

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

您可以考虑在gemspec中的依赖项上指定版本。

#1


6  

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).

问题是,在“lib/uncoil”中,您定义了类un线圈,而在“lib/uncoil/version”中,您定义了模块uncoil。如果该版本被加载,则会出现冲突,因为您试图重新打开一个实际上是模块的类(反之亦然,这取决于首先加载哪个类)。

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.

我假设当您运行rspec时,它根本不使用bundler(顺便说一句,由于您的依赖项没有被管理,所以您应该始终执行bundle exec rspec),因此它不会加载.gemspec,这是您需要版本文件的惟一位置。据推测,Bundler将加载gemspecs,进而加载版本文件,从而导致冲突。如果您以前没有经历过这种情况,我猜想您的一个依赖项(可能是guard-rspec)在更新的版本中被更改为装入Bundler。

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

您可以考虑在gemspec中的依赖项上指定版本。