设计,rspec和Mongoid测试失败

时间:2012-02-15 13:45:43

标签: ruby-on-rails ruby rspec mongoid

require 'spec_helper'

describe User do

  before(:each) do
    @attr = {
      :username => "User",
      :email => "aaaaer@example.com",
      :password => "foobar",
      :password_confirmation => "foobar",
      :phone_no => "0808322222"
    }
  end

  it "should create a new instance given a valid attribute" do
    User.create!(@attr)
  end
end

测试仍然失败,请不要知道为什么

Failures:

  1) User should create a new instance given a valid attribute
     Failure/Error: User.create!(@attr)
     Mongoid::Errors::Validations:
       Validation failed - Phone no can't be blank, Username can't be blank.
     # ./spec/models/user_spec.rb:16:in `block (2 levels) in <top (required)>'

Finished in 0.2505 seconds
2 examples, 1 failure

1 个答案:

答案 0 :(得分:1)

您的问题是您通过#create方法定义一些数据,其中数据不是attr_accessible属性。

所以你可以在你的attr_accessible列表中添加这个属性,你可以避免使用mass-assignement示例:

  it "should create a new instance given a valid attribute" do
    u = User.new
    @attr.each do |k,v|
      u.send("#{k}=", v)
    end
    u.save!
  end