Wednesday, 15 February 2012

ruby on rails - Why is this rspec request spec not updating the model? -



ruby on rails - Why is this rspec request spec not updating the model? -

i have requests spec interactions user model. want create sure users admin role can create/edit/destroy users. i'm having problem right edit action not update user. works when manually go through actions on site itself, tests fail update user.

here's spec:

it 'edits user' @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 alter { user_to_edit.last_name }.from("smith").to("changed") page.should have_content "john changed" end

the error is:

failure/error: expect{ result should have been changed "changed", "smith"

if alter lastly few lines of test this:

fill_in 'user_last_name', with: "changed" click_button "do it" page.should have_content "john changed"

then test succeeds. doesn't seem right, since page should not display "john changed" if user_to_edit not updated.

my delete request spec works fine:

it "deletes user" @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}" click_link 'delete' end }.to change(user,:count).by(-1) page.should_not have_content "john smith" end

i have user model:

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

i have factory:

require 'faker' factorygirl.define mill :user |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

i have these actions in user controller:

def edit @user = user.find_by_id(params[:id]) respond_to |format| format.html end end def update if params[:user][:password].blank? [:password,:password_confirmation].collect{|p| params[:user].delete(p) } end respond_to |format| if @user.errors[:base].empty? , @user.update_attributes(params[:user]) flash.now[:notice] = "your business relationship has been updated" format.html { render :action => :show } else format.html { render :action => :edit, :status => :unprocessable_entity } end end end

the routes.rb file relevant, since i'm using devise , have custom users controller:

devise_for :users, :skip => [:sessions, :registrations] devise_scope :user "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 "signup" => "devise/registrations#new", :as => :new_user_registration set "update-registration" => "devise/registrations#update", :as => :update_user_registration delete "delete-registration" => "devise/registrations#destroy", :as => :delete_user_registration "edit-registration" => "devise/registrations#edit", :as => :edit_user_registration "cancel-registration" => "devise/registrations#cancel", :as => :cancel_user_registration post "create-registration" => "devise/registrations#create", :as => :user_registration end resources :users, :controller => "users"

you fooled how smart testing frameworks :) certainly expect db entry user_to_edit change. user_to_edit local variable user_to_edit.last_name not alter no matter buttons click. seek { user_to_edit.reload.last_name }

ruby-on-rails devise capybara cancan rspec-rails

No comments:

Post a Comment