2013-02-11 62 views
8

मेरे पास उपयोगकर्ता मॉडल के साथ इंटरैक्शन के लिए अनुरोध अनुरोध है। मैं यह सुनिश्चित करना चाहता हूं कि व्यवस्थापक भूमिका वाले उपयोगकर्ता उपयोगकर्ता बना/संपादित/नष्ट कर सकें। मुझे अभी एक समस्या है जहां संपादन कार्रवाई उपयोगकर्ता को अद्यतन नहीं करती है। जब मैं मैन्युअल रूप से साइट पर कार्रवाइयों से गुजरता हूं, तो सब ठीक से काम करता है, लेकिन परीक्षण उपयोगकर्ता को अपडेट करने में विफल रहता है।यह आरएसपीईसी अनुरोध मॉडल को अपडेट नहीं कर रहा है?

यहाँ मेरी कल्पना है:

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 

त्रुटि है कि मैं मिलता है:

fill_in 'user_last_name', with: "Changed" 
    click_button "Do it" 
    page.should have_content "John Changed" 

तब:

Failure/Error: expect{ 
     result should have been changed to "Changed", but is now "Smith" 

यदि मैं यह करने के लिए परीक्षण के अंतिम कुछ पंक्तियों में परिवर्तन परीक्षण सफल होता है। यह सही प्रतीत नहीं होता है, क्योंकि पृष्ठ को "जॉन चेंज" प्रदर्शित नहीं करना चाहिए यदि user_to_edit अपडेट नहीं किया गया था।

मेरे हटाएँ अनुरोध कल्पना ठीक काम करता है:

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 

मैं एक उपयोगकर्ता मॉडल है:

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 

मैं अपने उपयोगकर्ता में इन कार्यों है:

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 

मैं इस कारखाने है नियंत्रक:

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 

routes.rb फ़ाइल के बाद से मैं वसीयत उपयोग कर रहा हूँ और एक कस्टम उपयोगकर्ता नियंत्रक है, प्रासंगिक भी है: आप कैसे स्मार्ट परीक्षण चौखटे देखो :) द्वारा मूर्ख कर रहे हैं

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" 
+1

के साथ प्रयास करें क्या आपके पास परीक्षण विफल होने के बारे में कोई और जानकारी है? उदाहरण के लिए, अपडेट के बाद, अद्यतन से पहले कंसोल पर @ user.errors को प्रिंट करना, और यदि अपडेट विफल हो जाए तो यह एक अच्छा विचार होगा। – Max

उत्तर

18

निश्चित रूप से आप db प्रवेश की उम्मीद बदलने के लिए user_to_edit के लिएuser_to_edit एक स्थानीय चर है इसलिए user_to_edit.last_name कोई फर्क नहीं पड़ता कि आप कौन से बटन क्लिक करते हैं। { user_to_edit.reload.last_name }

+0

उह। बेशक! अब यह स्पष्ट है कि आप इसे इंगित करते हैं - धन्यवाद! साथ ही, 'user_to_edit.reload.last_name' सुझाव भी शामिल करने के लिए धन्यवाद - आपने मुझे वहां बहुत सारी परेशानी भी बचाई है। –

+0

आपने अभी मुझे घंटे बचाया है, इस –

+0

के लिए धन्यवाद मुझे भी घंटे बचाया .. कमाल! – Aitizazk