0

在从 Rails 4.0 升级到 4.1 之前,我在没有 Spring 的情况下使用 RSpec/Guard,并且我的所有测试都通过了,除了绿点和绿色文本之外没有任何输出。在升级和安装 Spring 以及我认为需要的其他任何东西之后,我在通过的测试中遇到错误,有时我的测试由于“唯一性”错误而没有通过。

通过和失败测试之间的常见错误:

An error occurred in an after(:all) hook.
  NameError: uninitialized constant Recipe
  occurred at /home/user/.rvm/gems/ruby-2.1.0@global/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:238:in 'const_get'

现在 after(:all) 钩子简单地删除了所有 Manager::Wine 对象(和一个 Manager::Wine 对象 has_many recipes)。Manager 是一个 Rails 引擎,它有 Wine 和 Recipe 模型。Wine 类中没有任何地方明确指定 Recipe 常量。但正如我之前所说,在我升级和安装 Spring 之前,这个错误并没有出现在常规的 RSpec/Guard 测试中。

其他失败是“已采用属性”的结果。例如,标准的“有效工厂”测试因此在 create() 时失败。但我在 RSpec 中指定:

config.before(:each) do
    DatabaseCleaner.start
end

而且我在春季之前的随机测试中没有遇到这些错误。

宝石文件片段:

group :development, :test do
    gem 'spring'
    gem 'rspec-rails'
    gem 'guard'
    gem 'guard-rspec', require: false
    gem 'spring-commands-rspec'
    gem 'factory_girl_rails'
    gem 'database_cleaner'
end

Guardfile 包含:

guard :rspec, cmd:"spring rspec" do

Rails app_root/bin 包含文件:

rails
rake
rspec
spring

???

4

1 回答 1

0

Really strange. I wasn't notified of this problem before, but the application no longer was able to determine the HABTM relationship between Wines and Recipes. I updated my models to include the join_table argument and I no longer get the 'undefined constant' errors. I only found this out because I loaded the app in the browser and got a better error message to deal with. Still getting problems with 'duplicate entry' issues.

Also, I removed the destroy_all calls on each model as I think it's unnecessary considering DatabaseCleaner runs after each task.

于 2014-04-19T02:05:34.553 回答