ruby-on-rails - 获取 Rspec 错误没有使用 Mongoid 将 Symbol 隐式转换为 Integer

标签 ruby-on-rails ruby mongodb rspec mongoid

我想用 Rspec 测试我的 Rails 应用程序,但我收到了一个 no implicit conversion of Symbol into Integer 错误,没有任何明显的原因。根据我得到的回溯,我认为问题与 Mongo/Mongoid 有关,但是,我无法弄清楚它到底是什么。该代码在生产中完美运行。该错误仅在测试时发生。

简单看一下没有其他方法的模型:

class Card
  include Mongoid::Document
  field :front, type: String
  field :back, type: String
  field :level, type: Integer, default: 1
  field :review_date, type: DateTime, default: DateTime.now

  has_many :card_statistic, dependent: :destroy
  belongs_to :topic
  belongs_to :user

  validates :front, :back, :level, presence: true
  validates :topic, presence: { is: true, message: "must belong to a topic." }
  validates :user, presence: { is: true, message: "must belong to a user." }
  validates :level, numericality: { only_integer: true, greater_than: 0 }
end

模型中触发错误的一个函数:

def self.reset(card)
    card.update(level: 1)
end

测试代码:

it "puts the given card in level 1" do
    card = create(:card)
    Card.correct card
    card.reload
    Card.correct card
    card.reload
    expect(card.level).to eq(3)
    card.reset
    card.reload
    expect(card.level).to eq(1)
  end

然后,我得到的错误回溯:

1) Card puts the given card in level 1
     Failure/Error: Card.reset card
     TypeError:
       no implicit conversion of Symbol into Integer
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
     # /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
     # ./app/models/card.rb:57:in `reset'
     # ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'

测试 Controller 时也会触发错误。即使执行 get :index 也会抛出错误。预先感谢您的帮助。

最佳答案

显然,Mongoid 的最新版本存在一些问题,我直接从存储库中提取代码,而没有指定稳定版本。因此解决方案是要么将版本降级到 4.0,要么明确使用 5.0 beta 版本。

要么

gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'

gem 'mongoid', '~> 4'

代替

gem 'mongoid', '~> 5', github: 'mongoid/mongoid'

关于ruby-on-rails - 获取 Rspec 错误没有使用 Mongoid 将 Symbol 隐式转换为 Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31059008/

相关文章:

ruby-on-rails - 简化的 'logical' 模型可以干净地访问高度规范化的数据库

ruby-on-rails - 为什么这个作用域的 lambda 后面跟着一个 block ?

ruby-on-rails - ruby group_by 对象?

ruby-on-rails - Guard 无法识别 Sublime Text 3 文件更改

css - 无法运行 Bootstrap : couldn't find file 'bootstrap-sprockets' with type 'application/javascript'

ruby - 安装 gem 时无法找到 libxml2

javascript - 在 mongodb 中使用 $inc 和 $addToSet

mongodb - 如果我的 MongoDB 中禁用了脚本,我该如何测试

javascript - mongodb - 没有本地的聚合查找

java.lang.invoke.MethodHandleStatics.newInternalError(MethodHandleStatics.java :97)