Tôi có thông số yêu cầu cho các tương tác với mô hình Người dùng. Tôi muốn đảm bảo rằng Người dùng có vai trò Quản trị viên có thể tạo/chỉnh sửa/hủy Người dùng. Tôi đang gặp sự cố ngay bây giờ khi hành động Chỉnh sửa không cập nhật người dùng. Mọi thứ hoạt động đúng cách khi tôi tự thực hiện các hành động trên chính trang web đó, nhưng các kiểm tra không cập nhật được cho người dùng.Tại sao thông số yêu cầu rspec này không cập nhật mô hình?
Dưới đây là spec của tôi:
it 'edits a user' do
@user = FactoryGirl.create(:user)
visit new_user_session_path unless current_path == new_user_session_path
fill_in "Email", :with => @user.email
fill_in "Password", :with => @user.password
click_button "Sign In"
user_to_edit = FactoryGirl.create(:user, first_name: "John", last_name: "Smith")
visit edit_user_path(user_to_edit) unless current_path == edit_user_path(user_to_edit)
fill_in 'user_last_name', with: "Changed"
expect{
click_button "Do it"
}.to change { user_to_edit.last_name }.from("Smith").to("Changed")
page.should have_content "John Changed"
end
Các lỗi mà tôi nhận được là:
Failure/Error: expect{
result should have been changed to "Changed", but is now "Smith"
Nếu tôi thay đổi một vài dòng cuối cùng của thử nghiệm này:
fill_in 'user_last_name', with: "Changed"
click_button "Do it"
page.should have_content "John Changed"
Sau đó, thử nghiệm thành công. Điều này có vẻ không đúng, vì trang sẽ không hiển thị "John Changed" nếu user_to_edit
không được cập nhật.
Xóa yêu cầu đặc tả của tôi hoạt động tốt:
it "deletes a user" do
@user = FactoryGirl.create(:user)
visit new_user_session_path unless current_path == new_user_session_path
fill_in "Email", :with => @user.email
fill_in "Password", :with => @user.password
click_button "Sign In"
user_to_delete = FactoryGirl.create(:user, first_name: "John", last_name: "Smith")
visit users_path unless current_path == users_path
expect{
within ".user_#{user_to_delete.id}" do
click_link 'Delete'
end
}.to change(User,:count).by(-1)
page.should_not have_content "John Smith"
end
Tôi có một mô hình người dùng:
class User < ActiveRecord::Base
ROLES = %w[renter landlord admin]
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation :first_name, :last_name, :role
validates :password, :presence => true, :on => :create
validates :first_name, :presence => true
validates :last_name, :presence => true
before_save :set_phones
def set_phones
self.fax = Phoner::Phone.parse(self.fax).format("%a%n") unless self.fax.blank?
self.land_phone = Phoner::Phone.parse(self.land_phone).format("%a%n") unless land_phone.blank?
self.mobile_phone = Phoner::Phone.parse(self.mobile_phone).format("%a%n") unless mobile_phone.blank?
end
end
tôi có nhà máy này:
require 'faker'
FactoryGirl.define do
factory :user do |f|
f.first_name { Faker::Name.first_name }
f.last_name { Faker::Name.last_name }
f.email {Faker::Internet.email}
f.password { "oq2847hrowihgfoigq278o4r7qgo4" }
f.role { "admin" }
end
end
tôi có những hành động này ở người dùng của tôi bộ điều khiển:
def edit
@user = User.find_by_id(params[:id])
respond_to do |format|
format.html
end
end
def update
if params[:user][:password].blank?
[:password,:password_confirmation].collect{|p| params[:user].delete(p) }
end
respond_to do |format|
if @user.errors[:base].empty? and @user.update_attributes(params[:user])
flash.now[:notice] = "Your account has been updated"
format.html { render :action => :show }
else
format.html { render :action => :edit, :status => :unprocessable_entity }
end
end
end
File routes.rb
cũng là có liên quan, kể từ khi tôi đang sử dụng lập mưu và có một điều khiển người dùng tùy chỉnh:
devise_for :users, :skip => [:sessions, :registrations]
devise_scope :user do
get "login" => "devise/sessions#new", :as => :new_user_session
post 'login' => 'devise/sessions#create', :as => :user_session
delete "logout" => "devise/sessions#destroy", :as => :destroy_user_session
get "signup" => "devise/registrations#new", :as => :new_user_registration
put "update-registration" => "devise/registrations#update", :as => :update_user_registration
delete "delete-registration" => "devise/registrations#destroy", :as => :delete_user_registration
get "edit-registration" => "devise/registrations#edit", :as => :edit_user_registration
get "cancel-registration" => "devise/registrations#cancel", :as => :cancel_user_registration
post "create-registration" => "devise/registrations#create", :as => :user_registration
end
resources :users, :controller => "users"
Bạn có thêm thông tin nào về lý do thử nghiệm không thành công không? Ví dụ: có thể in ra @ user.errors vào bảng điều khiển trước khi cập nhật, sau khi cập nhật và nếu bản cập nhật không thành công thì sẽ là một ý tưởng hay. – Max