【发布时间】:2016-12-02 14:41:33
【问题描述】:
其实我想测试一个模型回调。
system_details.rb(模型)
class SystemDetail < ActiveRecord::Base
belongs_to :user
attr_accessible :user_agent
before_create :prevent_if_same
def agent
Browser.new(ua: user_agent, accept_language: 'en-us')
end
def prevent_if_same
rec = user.system_details.order('updated_at desc').first
return true unless rec
if rec.user_agent == user_agent
rec.touch
return false
end
end
end
prevent_if_same 方法工作正常并且按预期工作,但是当它返回 false 时会引发异常 ActiveRecord::RecordNotSaved,并且异常会破坏 rspec test。我想要的是,它应该默默地取消保存而不引发异常。
system_detail_spec.rb (rspec)
require 'rails_helper'
RSpec.describe SystemDetail, :type => :model do
context '#agent' do
it 'Checks browser instance' do
expect(SystemDetail.new.agent).to be_an_instance_of(Browser)
end
end
context '#callback' do
it 'Ensure not creating consecutive duplicate records' do
user = create :end_user
system_detail = create :system_detail, :similar_agent, user_id: user.id
updated_at = system_detail.updated_at
system_detail2 = create :system_detail, :similar_agent, user_id: user.id
system_detail.reload
expect(system_detail2.id).to be_nil
expect(system_detail.updated_at).to be > updated_at
end
end
end
第二次测试#callback 因异常而失败。
失败:
1) SystemDetail#callback 确保不创建重复记录 失败/错误:system_detail2 = 创建:system_detail,:similar_agent,user_id:user.id ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
有什么方法可以在不引发异常的情况下静默取消保存?
【问题讨论】:
标签: ruby-on-rails ruby activerecord rspec callback