0

我正在尝试使用 RSpec为Devise 2.2.2编写注册规范。我像往常一样准备了模型。User

# app/models/user.rb
class User < ActiveRecord::Base
  attr_accessible :role_ids, :as => :admin
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me

  rolify

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :token_authenticatable

  after_create :add_default_role

  def add_default_role
    add_role :user
  end
end

在规范中,我需要user使用password和进行注册,password_confirmation如下所示。

# spec/requests/users/sign_up_spec.rb
describe 'Sign up' do

  before(:each) do
    sign_out
  end

  it 'signs up a visitor with valid data' do
    user  = build(:user)

    click_link I18n.t('layouts.navigation.sign_up')
    fill_in I18n.t('devise.registrations.new.name'), with: user.name
    fill_in I18n.t('devise.registrations.new.email'), with: user.email

    find('.js-password').set user.password
    find('.js-password-confirmation').set user.password_confirmation

    click_button I18n.t('devise.registrations.new.submit')

    expect(page).to have_content I18n.t('layouts.navigation.sign_out')
    expect(page).not_to have_content I18n.t('layouts.navigation.sign_up')
    expect(page).not_to have_content I18n.t('layouts.navigation.sign_in')
  end
end

这是用于呈现页面的设计视图模板:

<!-- app/views/devise/registrations/new.html.erb -->
<h2><%= t('.title', :default => "Sign up") %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :name, t('.name', :default => "Name" ) %><br />
  <%= f.email_field :name %></div>

  <div><%= f.label :email, t('.email', :default => "Email" ) %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :user_password, t('.user_password', :default => "Password" ) %><br />
  <%= f.password_field :user_password, class: 'js-password' %></div>

  <div><%= f.label :user_password_confirmation, t('.user_password_confirmation', :default => "Password confirmation" ) %><br />
  <%= f.password_field :user_password_confirmation, class: 'js-password-confirmation' %></div>

  <div><%= f.submit t('.submit', :default => "Submit") %></div>
<% end %>

尽管我将这两个属性都添加到了attr_accessible这个错误消息中:

Sign up signs up a visitor with valid data
  Failure/Error: click_button I18n.t('devise.registrations.new.submit')
  ActiveModel::MassAssignmentSecurity::Error:
    Can't mass-assign protected attributes: user_password, user_password_confirmation
   # ./spec/requests/users/sign_up_spec.rb:20:in `block (2 levels) in <top (required)>'
4

1 回答 1

2

表单中输入的名称错误,应该是user[password]& user[password_confirmation],但错误是说你有user_password&user_password_confirmation


可以在这里看到视图模板中的相关更改:

<div><%= f.label :password, t('.password', :default => "Password" ) %><br />
<%= f.password_field :password, class: 'js-password' %></div>

<div><%= f.label :password_confirmation, t('.password_confirmation', :default => "Password confirmation" ) %><br />
<%= f.password_field :password_confirmation, class: 'js-password-confirmation' %></div>
于 2013-03-13T22:59:12.257 回答