Devise、rspec 和 Mongoid 测试失败

发布于 2025-01-05 15:41:53 字数 921 浏览 0 评论 0原文

require 'spec_helper'

describe User do

  before(:each) do
    @attr = {
      :username => "User",
      :email => "[email protected]",
      :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
require 'spec_helper'

describe User do

  before(:each) do
    @attr = {
      :username => "User",
      :email => "[email protected]",
      :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

The test keep failing, please dont know why

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救赎№ 2025-01-12 15:41:53

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

因此,您可以将此属性添加到 attr_accessible 列表中,这样您就可以通过示例避免使用批量分配:

  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

You problem is you define some data by #create method where data is not is attr_accessible attribute.

So you can add this attribute on your attr_accessible list of you can avoid using the mass-assignement by example :

  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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文