任何有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,但那沒有做任何事情。我覺得這與模型有某種限制有關,不允許創建用戶,除非有密碼確認。但我不太確定。如果有人能幫忙,我會很感激。
創建另一個字段是我正在建議的。測試日誌在您執行#create操作時會說明什麼? (與其他領域,我的意思是,因爲你將需要這個。) – bgates