Ruby on rails rspec希望在通过post创建记录后更改记录值
我有一个例子,通过传递的Ruby on rails rspec希望在通过post创建记录后更改记录值,ruby-on-rails,rspec,Ruby On Rails,Rspec,我有一个例子,通过传递的post创建一个新记录 describe 'POST create' do let(:schedule_child) { FactoryGirl.create(:schedule_child) } let(:post_queue) { post :create, schedule_child_id: schedule_child.id, format: :js } it { expect{post_queue}.to change(PatientQueue,
post
创建一个新记录
describe 'POST create' do
let(:schedule_child) { FactoryGirl.create(:schedule_child) }
let(:post_queue) { post :create, schedule_child_id: schedule_child.id, format: :js }
it { expect{post_queue}.to change(PatientQueue, :count).by(1) }
end
我有一个属性,PatientQueue.queue\u number
,每次添加新记录时,该属性将增加1。现在我想看看这个属性是否发生了变化
it { expect{post_queue}.to change(PatientQueue, :queue_number).by(1) }
但这是我得到的
NoMethodError: undefined method `queue_number' for #<Class:0x0000000849e780>
PatientQueue
是一个activerecord类,它有一个方法count
post\u queue
是该类的一个实例,具有方法queue\u number
该类没有与实例相同的方法,因此您可以像change(post\u queue,:queue\u number)那样编写测试
然而,测试有点难,你能告诉我们你的数据模型关系吗?如果一个PatientQueue
有很多schedule\u child
,也许您只想使用rails缓存计数器 您好,House 9,我添加了modelPatientQueue
,是的,它有许多ScheduleChild
。我尝试使用change(post\u queue,:queue\u number).by(1)
得到了相同的错误。我不太确定如何在我的案例中实现cache_计数器,因为新记录是由用户添加的,用户将根据队列编号
class PatientQueue < ActiveRecord::Base
# Validations
validates :patient, :schedule_child, presence: true
validate :is_not_exist
# Relations
belongs_to :schedule_child
belongs_to :patient
before_create :insert_queue_number
def is_exist?
PatientQueue.find_by_schedule_child_id_and_patient_id(schedule_child_id, patient_id).present?
end
private
def insert_queue_number
last_id = PatientQueue.where("schedule_child_id = ?", self.schedule_child_id).count
self.queue_number = last_id + 1
end
def is_not_exist
errors.add(:schedule_child, :is_exist) if is_exist?
end
end