【发布时间】:2021-01-28 15:53:36
【问题描述】:
由于某种原因,我的一个模型的 Rspec 模型测试失败,原因是正在调用自定义验证方法,但在测试中找不到。至少这是我认为正在发生的事情。当提交表单以在应用程序中创建新恐龙时触发此验证。谁能告诉我为什么会发生这种情况以及可能的解决方法是什么?
这是所有 4 个测试的失败错误:
Failure/Error: if cage.at_capacity?
NoMethodError:
undefined method `at_capacity?' for nil:NilClass
模型/dinosaur.rb
class Dinosaur < ApplicationRecord
belongs_to :cage
validates :name, :species, :diet_type, :cage_id, presence: true
validates_uniqueness_of :name
validate :is_cage_at_capacity
validate :is_cage_powered_down
validate :cage_contains_diet_mismatch
=begin
def set_cage(c)
return false if c.at_capacity?
cage = c
end
def move_dino_to_powered_down_cage(c)
return false if c.is_powered_down?
cage = c
end
=end
def is_herbivore?
return diet_type == "Herbivore"
end
def is_carnivore?
return diet_type == "Carnivore"
end
def is_cage_powered_down
if cage.is_powered_down?
errors.add(:cage_id, "Chosen cage is powered down. Please choose another cage!")
end
end
def is_cage_at_capacity
if cage.at_capacity?
errors.add(:cage_id, "Chosen cage is full. Please choose another cage!")
end
end
def cage_contains_diet_mismatch
if cage.has_carnivore == true and is_herbivore?
errors.add(:cage_id, "Chosen cage contains carnivores! This dinosaur will be eaten!")
else
if cage.has_herbivore == true and is_carnivore?
errors.add(:cage_id, "Chosen cage contains herbivores! This dinosaur will eat the others!")
end
end
end
end
spec/models/dinosaur_spec.rb
require 'rails_helper'
describe Dinosaur, type: :model do
it "is valid with valid attributes" do
dinosaur = Dinosaur.new(name:"Yellow", species:"Tyrranosaurus", diet_type:"Carnivore", cage_id: 7)
expect(dinosaur).to be_valid
end
it "is not valid without a name" do
dinosaur = Dinosaur.new(name: nil)
expect(dinosaur).to_not be_valid
end
it "is not valid without a max capacity" do
dinosaur = Dinosaur.new(species: nil)
expect(dinosaur).to_not be_valid
end
it "is not valid without a power status" do
dinosaur = Dinosaur.new(diet_type: nil)
expect(dinosaur).to_not be_valid
end
end
【问题讨论】:
标签: ruby rspec-rails ruby-on-rails-6