0

我正在尝试使用 rspec 测试我的管理模型,但我遇到了错误如何解决此错误,请给我一些想法。

我的模型/admin_spec.rb 是

require 'spec_helper'

describe Admin do
  before do
    @admin = Admin.new(email: "superuser@superuser.com", hash: "iJXdppRiI52XsqJe", password: "736fc7f1f4f382e79f2278817d056a0ec14b9ff5", city_id: "", role_id: 4, request_hash: "", status: 1)
  end

  subject { @admin }

  it { should respond_to :email }
  it { should respond_to :password }
  it { should respond_to :phone }
  it { should be_valid }
end

错误是:

) Admin phone with valid format 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/admin_spec.rb:15:in `block (3 levels) in <top (required)>'


Finished in 16.08 seconds
34 examples, 1 failure

Failed examples:

rspec ./spec/models/admin_spec.rb:15 # Admin phone with valid format 

Randomized with seed 31486
4

1 回答 1

2

看起来您对 Admin 的 phone 属性进行了验证,但您从未在测试主题中设置它。尝试使用有效的电话属性将其设置在 before 块内,测试应该通过。

before do
    @admin = Admin.new(phone: "01234568", email: "superuser@superuser.com", hash: "iJXdppRiI52XsqJe", password: "736fc7f1f4f382e79f2278817d056a0ec14b9ff5", city_id: "", role_id: 4, request_hash: "", status: 1)
end
于 2012-11-26T11:16:31.380 回答