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.
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
精彩评论