【发布时间】:2016-09-21 08:49:39
【问题描述】:
我是 Rails 5 的新手,正在尝试测试一个极其简单的数据库模型,但我似乎无法做到正确。我正在尝试测试has_many 关联。
模型代码如下:
app/models/database_models/response.rb
module DatabaseModels
class Response < ApplicationRecord
has_many :question_responses, :class_name => 'DatabaseModels::
QuestionResponse'
end
end
规范(RSpec)看起来像这样
spec/models/screener_response_spec.rb
require 'rails_helper'
describe DatabaseModels::Response, type: :model do
it { is_expected.to have_many(:question_responses) }
end
它失败了:
expected #<DatabaseModels::Response:0x007fd91ccda210> to respond to `has_many?`
我做错了什么?
【问题讨论】:
-
您是否包含了 shoulda gem?据我所知,只有这个 gem 提供了你正在使用的匹配器。
-
我的 Gemfile 中的 :test 组中确实有
gem 'shoulda-matchers'。 -
有一个开放的 Rails 5 PR github.com/thoughtbot/shoulda-matchers/pull/960 。我认为这个 gem 还没有准备好 Rails 5。
-
是否有另一种方法可以在没有
shoulda_matchers的情况下使用 RSpec 进行测试 -
expect(described_class.reflect_on_association(:question_responses).macro).to eq :has_many也应该适用于 Rails 5。
标签: ruby-on-rails rspec ruby-on-rails-5