【问题标题】:Rspec tests failing for phone number checkRspec 测试电话号码检查失败
【发布时间】: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


    【解决方案1】:

    您的测试现在返回 true,因为用户将拥有电话号码 +1-123-456-7890,而您的备用号码将是 123-456-7890。这是因为您在 primary_phone 前添加了一个额外的 +1-,而不是在 alternate_phone 前。

    你应该添加这个

    def alternate_phone=(num)
      write_attribute(:alternate_phone, self.number_to_phone(num, country_code: 1))
    end
    

    或将您的测试更改为

    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: '+1-123-456-7890').save).to be_falsey
    end
    

    【讨论】:

    • 你读过这个问题吗?我将相同的值传递给primary_phone 和alternate_phone(即+1-123-456-7890)并且我的测试失败了。
    • 您将primary_phone= 更改为在开头添加+1-,但您没有为alternate_phone 执行此操作。这意味着一旦您保存模型,它们就不同了。您的测试断言您无法保存模型,因为手机是相同的,而实际上它们不是。
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    相关资源
    最近更新 更多