ruby – 无法运行RSpec(未初始化的常量User(NameError)

前端之家收集整理的这篇文章主要介绍了ruby – 无法运行RSpec(未初始化的常量User(NameError)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试运行bundle exec rspec spec / models / user_spec.rb但未能执行(参见下面的错误).

user_spec.rb的内容

require 'rails_helper'

describe User do
  pending "add some examples to (or delete) #{__FILE__}"
end

如果我删除最后3行,则完成0个示例和0个失败.但是,当最后3行存在时,会产生错误

/spec/models/user_spec.rb:4:in `<top (required)>': uninitialized constant User (NameError)
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `block in load_spec_files'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `each'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/configuration.rb:1057:in `load_spec_files'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:97:in `setup'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:85:in `run'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:70:in `run'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/lib/rspec/core/runner.rb:38:in `invoke'
    from /var/lib/gems/1.9.1/gems/rspec-core-3.0.3/exe/rspec:4:in `<top (required)>'
    from /usr/local/bin/rspec:23:in `load'
    from /usr/local/bin/rspec:23:in `<main>'

它似乎不知道用户是什么,并将其视为常量,而这实际上是一个模型.我已经在ruby沙箱中验证了我可以在数据库中创建新用户.有任何想法吗?

回答:
从.rspec中删除–warnings后(感谢kirti),目前的错误是关于水豚版本太低(需要2.2).安装最新版本(修改gemfile和bundle install)后,rspec似乎正在正确执行.感谢大家的意见.

解决方法

我有同样的问题,这是由于rspec / rails_helper.rb没有在任何地方被调用.

我将它添加到.rspec文件中,所以我的文件看起来像这样

--color
--require spec_helper
--require rails_helper

这个并在spec / spec_helper.rb中添加警告为false解决了它!

config.warnings = false
原文链接:https://www.f2er.com/ruby/267472.html

猜你在找的Ruby相关文章