[英]Ruby on Rails with RSpec and shoulda-matcher validate_inclusion_of giving NoMethodError
編寫規范時,我遇到了validate_inclusion_of匹配器的問題。
我目前的規格與用戶和用戶組有關。 用戶具有用戶組ID。
我想檢查用戶的用戶組ID是否實際位於當前用戶組ID列表中。
目前我的規格基本上是:
describe 'company_user_group list of valid IDs' do
let!(:company_user_group_everything) { FactoryGirl.create(:company_user_group_everything) }
let!(:company_user_group_nothing) { FactoryGirl.create(:company_user_group_nothing) }
it '' do
@company_user = FactoryGirl.create(:company_user, id: 1)
CompanyUser.current_id = @company_user.id
is_expected.to validate_inclusion_of(:company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)
end
end
但我得到的錯誤是:
1) CompanyUser validations company_user_group list of valid IDs should validate that :company_user_group_id is either ‹2› or ‹3›
Failure/Error: is_expected.to validate_inclusion_of(:company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)
NoMethodError:
undefined method `attribute_setter' for nil:NilClass
我嘗試了各種不同的東西,並使用byebug等進行了調試,但沒有什么對我有用。
例如
加入
@company_user_group_id = @company_user.company_user_group_id
並將is_expected.to行更改為
is_expected.to validate_inclusion_of(@company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)
我收到以下錯誤
1) CompanyUser validations company_user_group list of valid IDs should validate that :8 is either ‹8› or ‹9›
Failure/Error: is_expected.to validate_inclusion_of(@company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)
Shoulda::Matchers::ActiveModel::AllowValueMatcher::AttributeDoesNotExistError:
The matcher attempted to set :8 on the CompanyUser to 8, but that
attribute does not exist
因此,似乎要檢查的組ID有效(例如8)並且數組有效(例如8和9)但匹配不起作用。
任何幫助非常感謝!
一些公司的用戶模型
# This model holds User identities for internal staff. This model is
# primarily intended for use in the xxxx application.
class CompanyUser LT ActiveRecord::Base
acts_as_authentic do |c|
c.merge_validates_uniqueness_of_email_field_options case_sensitive: false
c.merge_validates_length_of_password_field_options minimum: 8
if Rails.env.production?
c.logged_in_timeout = 30.minutes
else
c.logged_in_timeout = 90.minutes
end
end
# Constants
# relationships
belongs_to :company_user_group
# validations
validates :first_name, presence: true, length: { maximum: 20 }
validates :last_name, presence: true, length: { maximum: 20 }
validates :company_user_group_id, presence: true, numericality: { only_integer: true, greater_than: 0 }
validates :company_user_group_id, inclusion: { in: CompanyUserGroup.full_list_of_ids }, unless: 'Rails.env.test?'
validate :check_sys_admin_permission
如果刪除“除非:'Rails.env.test?'”位,大多數規范都會因某些未知原因而失敗。 剛才提到它是相關的。
以及公司組模型中的以下方法
# class methods
def self.full_list_of_ids
CompanyUserGroup.all.pluck(:id)
end
我想你想要這樣的東西:
it '' do
@company_user = FactoryGirl.create(:company_user, id: 1)
@company_user.company_user_group = company_user_group_everything
expect(@company_user).to validate_inclusion_of(:company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)
end
您甚至可能不需要設置公司用戶的公司用戶gropu。
但是你有
validates :company_user_group_id, inclusion: { in: CompanyUserGroup.full_list_of_ids }, unless: 'Rails.env.test?'
這肯定意味着它會失敗,因為你沒有在測試環境中這樣做
我已經弄清楚了。 出於某種原因,validate_inclusion_of在let內有效! 塊。
什么參數發送到let helper方法並不重要,只是那個。
仍然不知道為什么這個修復它,如果有人可以啟發我!?
所以,現在通過以下規范
describe 'company_user_group list of valid IDs' do
let!(:company_user_group_everything) { FactoryGirl.create(:company_user_group_everything) }
let!(:company_user_group_nothing) { FactoryGirl.create(:company_user_group_nothing) }
let!(:temp) do
it {is_expected.to validate_inclusion_of(:company_user_group_id).in_array(CompanyUserGroup.full_list_of_ids)}
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.