Ruby on rails Rails模型未能通过格式验证,regex
我正在为录音设置一个模型,具有以下约束Ruby on rails Rails模型未能通过格式验证,regex,ruby-on-rails,regex,model,rspec,tdd,Ruby On Rails,Regex,Model,Rspec,Tdd,我正在为录音设置一个模型,具有以下约束 class Recording < ActiveRecord::Base attr_accessible :agent_id, :confirmation, :filepath, :phone, :call_queue_id, :date belongs_to :call_queue PHONE_FORMAT = /^[0-9]+$|Unavailable/ validates_presence_of :call_queue_id
class Recording < ActiveRecord::Base
attr_accessible :agent_id, :confirmation, :filepath, :phone, :call_queue_id, :date
belongs_to :call_queue
PHONE_FORMAT = /^[0-9]+$|Unavailable/
validates_presence_of :call_queue_id, :agent_id, :phone, :filepath, :date
validates :phone, format: { with: PHONE_FORMAT }
end
前两项测试通过,但第三项测试失败,原因如下:
Failure/Error: @recording.should be_valid
expected valid? to return true, got false
我用测试我的正则表达式以确保它正常工作,然后再次使用irb来确保它正常工作。我真的很困惑为什么这会失败
编辑:
我最终通过在rspec中更改我的语句来传递规范:
describe Recording do
let(:queue) { FactoryGirl.create(:call_queue) }
before(:each) { @recording = queue.recordings.create(FactoryGirl.attributes_for(:recording) }
# the rest is the same...
这在某种程度上最终对我来说是有意义的。一切都变得一团糟(falses返回true,反之亦然)的原因是因为一旦某个属性使记录无效,我就无法再次更改它了吗?似乎情况就是这样,我只是想确定一下。试试:
PHONE_FORMAT = /^([0-9]+|Unavailable)$/
没有前斜杠?我需要任何类型的引号吗?我添加了正斜杠,得到了与相同的错误before@BradRice是的,那是一个打字错误,应该是斜杠(修正)。我似乎无法复制你所犯的错误。。。
PHONE_FORMAT = /^([0-9]+|Unavailable)$/