如何使用 RSpec 测试我的设备用户模型验证?

发布于 2024-11-06 06:36:31 字数 685 浏览 1 评论 0原文

我可以在 RSpec 中找到有关测试设备用户控制器和视图的建议。我还看到有人建议 devise gem 代码已经过测试,因此花费大量时间重新发明轮子是没有用的。

但是,我的用户模型还有其他字段需要在用户注册时进行验证。我在 user.rb 模型中使用标准 validates... 语句。例如:

validates_presence_of     :nickname

我尝试在 user_spec.rb 中使用简单的验证测试,但是当我尝试像这样创建用户时:

record = Factory.create(:user)

我收到此错误:

undefined method `encode!' for "Confirmation":String

encode! 方法不是来自我的代码,它一定是 devise 正在使用的宝石之一,但我还没有找到它。

我尝试使用 User.new 和 Factory Girl 创建用户。无论哪种方式我都会遇到同样的错误。这个规范一直在通过,直到我更新了我的所有宝石。不幸的是我没有记录当时更新的所有内容。我已尝试将设备回滚到以前的版本,但仍然遇到相同的错误。

Rails 3、RSpec2

感谢您的建议。

I can find recommendations for testing devise user controllers and views in RSpec. I've also seen suggestions that the devise gem code is already tested so it's not useful to spend a lot of time reinventing the wheel.

However, my user model has other fields that I need validated when the user signs up. I'm using standard validates... statements in the user.rb model. For example:

validates_presence_of     :nickname

I'm trying to use simple validation testing in my user_spec.rb, but when I try to create the user like this:

record = Factory.create(:user)

I get this error:

undefined method `encode!' for "Confirmation":String

The encode! method is not coming from my code, it must be one of the gems that devise is using, but I haven't been able to find it, yet.

I've tried creating the user using User.new and Factory Girl. I get the same error either way. This spec was passing until I did an update of all my gems. Unfortunately I didn't keep a note of everything that got updated at the time. I've tried rolling devise back to previous versions but still get the same error.

Rails 3, RSpec2

Thanks for any advice.

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

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

发布评论

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

评论(1

掐死时间 2024-11-13 06:36:31

看起来不错,也许是,我的测试代码可以帮助你:

user_spec.rb

require 'spec_helper'

describe User do
  before :each do
    @user = Factory.build(:user)
  end

  it "should not be valid without a first_name" do
    @user.first_name = nil
    @user.should_not be_valid
  end

end

user.rb (Model)

class User < ActiveRecord::Base

  validates_presence_of :first_name

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable, :lockable,
         :recoverable, :rememberable, :trackable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :login, :first_name, :email, :password, :password_confirmation, :remember_me
  attr_accessor :login

  devise :database_authenticatable, :recoverable, :validatable

  protected

  def password_required?
    !persisted? || password.present? || password_confirmation.present?
  end



end

It seems to be fine, may be, my testing code helps you out:

user_spec.rb

require 'spec_helper'

describe User do
  before :each do
    @user = Factory.build(:user)
  end

  it "should not be valid without a first_name" do
    @user.first_name = nil
    @user.should_not be_valid
  end

end

user.rb (Model)

class User < ActiveRecord::Base

  validates_presence_of :first_name

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, :lockable and :timeoutable
  devise :database_authenticatable, :registerable, :lockable,
         :recoverable, :rememberable, :trackable
  # Setup accessible (or protected) attributes for your model
  attr_accessible :login, :first_name, :email, :password, :password_confirmation, :remember_me
  attr_accessor :login

  devise :database_authenticatable, :recoverable, :validatable

  protected

  def password_required?
    !persisted? || password.present? || password_confirmation.present?
  end



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