1

任何有Rails 4經驗的人都可以幫助我。我目前正在閱讀「Rails 4 in Action」,並且無法通過本書的一個規範(特別是從章節7.5:基本訪問控制 - 基於命名空間的CRUD)。 我有一個規範創建一個具有管理權限的FactoryGirl用戶,並嘗試創建一個具有管理權限的新用戶。密碼確認不能爲空

spec/features/admin/creating_users_spec.rb 
require 'spec_helper' 

feature "Creating Users" do 
    let!(:admin) { FactoryGirl.create(:admin_user) } 
    before do 
    sign_in_as!(admin) 

    visit '/' 
    click_link "Admin" 
    click_link "Users" 
    click_link "New User" 
    end 

    scenario 'Creating a new user' do 
    fill_in "Email", with: "[email protected]" 
    fill_in "Password", with: "hunter2" 
    click_button "Create User" 
    expect(page).to have_content("User has been created.") 
    end 
end 

當我運行這個天賦,我相信我不斷收到一個「確認密碼不能爲空錯誤」。

`$ bin/rspec spec/features/admin/creating_users_spec.rb` 

    DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade 
    documentation to learn more about this new config option. (called from get at 
    /home/sean/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/forwardable.rb:171) 
    F 

    Failures: 

    1) Creating Users Creating a new user 
    Failure/Error: expect(page).to have_content("User has been created.") 
     expected there to be text "User has been created." in "Signed in as: username Admin Sign out User has not been created. New User 1 error prohibited this user from being saved: Password confirmation can't be blank Email Password Is an admin?" 
    # ./spec/features/admin/creating_users_spec.rb:19:in `block (2 levels) in <top (required)>' 

Finished in 2.82 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/features/admin/creating_users_spec.rb:15 # Creating Users Creating a new user 

Randomized with seed 56202 

這裏是工廠,創建它:

FactoryGirl.define do 
    sequence(:email) {|n| "user#{n}example.com" } 

    factory :user do 
    name "username" 
    email { generate(:email) } 
    #email "[email protected]" 
    password "hunter2" 
    password_confirmation "hunter2" 

    factory :admin_user do 
     admin true 
    end 
    end 
end 

這是管理控制器:

class Admin::UsersController < Admin::BaseController 
    def new 
    @user = User.new 
    end 

    def index 
    @users = User.order(:email) 
    end 

    def create 
    @user = User.new(user_params) 
    if @user.save 
     flash[:notice] = "User has been created." 
     redirect_to admin_users_path 
    else 
     flash.now[:alert] = "User has not been created." 
     render action: "new" 
    end 
    end 

    private 
    def user_params 
     params.require(:user).permit(:name, :password, :password_confirmation) 
    end 
end 

這裏是用戶模型:

class User < ActiveRecord::Base 
    attr_accessible :name, :password, :password_confirmation 

    has_secure_password 
end 

而且那麼最後這裏是管理視圖表單,其中a這個信息被填充:

<%= form_for [:admin, @user] do |f| %> 
    <% if @user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@user.errors.count, "error") %> prohibited 
     this user from being saved:</h2> 
     <ul> 
     <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <p> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    </p> 
    <p> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    </p> 
    <p> 
    <%= f.check_box :admin %> 
    <%= f.label :admin, "Is an admin?" %> 
    </p> 
    <%= f.submit %> 
<% end %> 

我不知道如何解決這個問題。我已經嘗試在表單中創建另一個字段以進行密碼確認,然後在規範中包含了一個fill_in,但那沒有做任何事情。我覺得這與模型有某種限制有關,不允許創建用戶,除非有密碼確認。但我不太確定。如果有人能幫忙,我會很感激。

+0

創建另一個字段是我正在建議的。測試日誌在您執行#create操作時會說明什麼? (與其他領域,我的意思是,因爲你將需要這個。) – bgates

回答

0

I've tried creating another field in the form for a password confirmation, and then included a fill_in within the spec, but that didn't do anything.

看跌,早在,然後刪除用戶模型整個attr_accessible線。

默認情況下,Rails 4中允許批量分配的屬性作爲它的一部分strong parameters策略 - 您不必在模型中明確列入白名單。

如果這不起作用,請發佈更新後的視圖,包括新的password_confirmation表單字段和規範。

+0

這工作。更詳細的解釋如下: – Alphonse23

0

工作正常!

bin/rspec spec/features/admin/creating_users_spec.rb 
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from get at /home/sean/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/forwardable.rb:171) 
. 

Finished in 0.44082 seconds 
1 example, 0 failures 

Randomized with seed 45288 

這裏的規格:

require 'spec_helper' 

feature "Creating Users" do 
    let!(:admin) { FactoryGirl.create(:admin_user) } 
    before do 
    sign_in_as!(admin) 

    visit '/' 
    click_link "Admin" 
    click_link "Users" 
    click_link "New User" 
    end 

#=begin 
    scenario 'Creating a new user' do 
    fill_in "Email", with: "[email protected]" 
    fill_in "user[password]", with: "hunter2" 
    fill_in "Password confirmation", with: "hunter2" 
    click_button "Create User" 
    expect(page).to have_content("User has been created.") 
    end 
#=end 
=begin 
    scenario "Creating an admin user" do 
    fill_in "Email", with: "[email protected]" 
    fill_in "Password", with: "password" 
    check "Is an admin?" 
    click_button "Create User" 
    expect(page).to have_content("User has not been created") 
    within("#users") do 
     expect(page).to have_content("[email protected] (Admin)") 
    end 
    end 
=end 
end 

而這裏的觀點:

<%= form_for [:admin, @user] do |f| %> 
    <% if @user.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@user.errors.count, "error") %> prohibited 
     this user from being saved:</h2> 
     <ul> 
     <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <p> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    </p> 
    <p> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    </p> 
    <p> 
    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation %> 
    </p> 
    <p> 
    <%= f.check_box :admin %> 
    <%= f.label :admin, "Is an admin?" %> 
    </p> 
    <%= f.submit %> 
<% end %> 

雖然當我在用戶模式註釋掉attr_accessible,我得到了一個錯誤:

class User < ActiveRecord::Base 
    #attr_accessible :name, :password, :password_confirmation 

    has_secure_password 
end 

現在的規格如下:

bin/rspec spec/features/admin/creating_users_spec.rb 
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from get at /home/sean/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/forwardable.rb:171) 
F 

Failures: 

    1) Creating Users Creating a new user 
    Failure/Error: click_button "Create User" 
    ActiveModel::MassAssignmentSecurity::Error: 
     Can't mass-assign protected attributes for User: password, password_confirmation 
    # ./app/controllers/admin/users_controller.rb:11:in `create' 
    # ./spec/features/admin/creating_users_spec.rb:19:in `block (2 levels) in <top (required)>' 

Finished in 0.45317 seconds 
1 example, 1 failure 

Failed examples: 

rspec ./spec/features/admin/creating_users_spec.rb:15 # Creating Users Creating a new user 

Randomized with seed 7339 

所以,也許當我上次嘗試創建密碼確認字段時,我可能會留下一些東西,這一次我沒有 - 所以它的工作。

謝謝!