Ruby on rails 设计、rspec和Mongoid测试失败
考试一直不及格,请不知道为什么Ruby on rails 设计、rspec和Mongoid测试失败,ruby-on-rails,ruby,rspec,mongoid,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
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
故障:
1) 用户应在给定有效属性的情况下创建新实例
失败/错误:User.create!(@attr)
Mongoid::错误::验证:
验证失败-电话号码不能为空,用户名不能为空。
#./spec/models/user_spec.rb:16:in'block(2层)in'
以0.2505秒完成
2例,1例失败
您的问题是您通过“创建方法”定义了一些数据,其中数据不是属性可访问的属性
因此,您可以将此属性添加到属性列表中,您可以通过示例避免使用质量分配:
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
我们需要模型来知道你的错误在哪里。可能您使用attr\u accessible或attr_protected@shingara我已在我正在使用attr_accessible时粘贴了用户模型我使用了您提供的此代码段,出现以下错误:1)用户应在给定有效属性的情况下创建新实例失败/错误:u.send(k“=”,v)NoMethodError:undefined method
k'for#./spec/models/user_spec.rb:18:in
block(3层)in'./spec/models/user_spec.rb:17:ineach'./spec/models/user_spec.rb:17:in
block(2层)in'我注释掉了模型中的属性可访问行,测试通过了,所以我知道去哪里找,稍后我会修复它,非常抱歉,我用send(“#{k}=”,v)
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