【发布时间】:2015-10-26 08:25:53
【问题描述】:
我在user_spec.rb 中有以下代码,它运行良好
#spec/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, :type => :model do
it 'is invalid with an alternate phone number that is also that user\'s primary phone' do
expect(FactoryGirl.build(:user, primary_phone: '123-456-7890', alternate_phone: '123-456-7890').save).to be_falsey
end
end
直到我改变了下面的代码
#app/models/user.rb
def primary_phone=(num)
write_attribute(:primary_phone, self.number_to_phone(num))
end
到
#app/models/user.rb
def primary_phone=(num)
write_attribute(:primary_phone, self.number_to_phone(num, country_code: 1))
end
现在测试失败并出现以下错误
Failures:
1) User is invalid with an alternate phone number that is also that user's primary phone
Failure/Error: expect(FactoryGirl.build(:user, primary_phone: '123-456-7890', alternate_phone: '123-456-7890').save).to be_falsey
expected: falsey value
got: true
# ./spec/models/user_spec.rb:80:in `block (2 levels) in <top (required)>'
# ./spec/rails_helper.rb:62:in `block (3 levels) in <top (required)>'
# /home/pavan/.rvm/gems/ruby-2.2.1/gems/database_cleaner-1.3.0/lib/database_cleaner/generic/base.rb:15:in `cleaning'
# /home/pavan/.rvm/gems/ruby-2.2.1/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:92:in `cleaning'
# /home/pavan/.rvm/gems/ruby-2.2.1/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:86:in `block (2 levels) in cleaning'
# /home/pavan/.rvm/gems/ruby-2.2.1/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:87:in `call'
# /home/pavan/.rvm/gems/ruby-2.2.1/gems/database_cleaner-1.3.0/lib/database_cleaner/configuration.rb:87:in `cleaning'
# ./spec/rails_helper.rb:61:in `block (2 levels) in <top (required)>'
Finished in 19.04 seconds (files took 17.2 seconds to load)
118 examples, 1 failure, 70 pending
我认为更改的电话号码格式导致问题并更改了
it 'is invalid with an alternate phone number that is also that user\'s primary phone' do
expect(FactoryGirl.build(:user, primary_phone: '123-456-7890', alternate_phone: '123-456-7890').save).to be_falsey
end
到
it 'is invalid with an alternate phone number that is also that user\'s primary phone' do
expect(FactoryGirl.build(:user, primary_phone: '+1-123-456-7890', alternate_phone: '+1-123-456-7890').save).to be_falsey
end
但错误存在。我不知道发生了什么事。非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec