Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flash[:notice].should_not be_nil failed in rspec

Here is rspec code for controller:

it "should render edit if update was not saved" do
  item = Factory(:lease_item)
  session[:corp_head] = true
  post 'update', {:id => item.id, :name => 'nil'}
  flash[:notice].should_not be_nil
  response.should render 'edit'    
end

The update in controller is:

  def update
    if (eng? && dept_head?) || corp_head? || ceo?
      @lease_item = LeaseItem.find(params[:id])
      @lease_item.input_by_id = session[:user_id]
      if @lease_item.update_attributes(params[:lease_item], :as => :roles_update)

        #redirect
        redirect_to URI.escape("/view_handler?index=0&msg=Lease item updated successfully")
      else
        #back to new
        render 'edit', :notice => "Item NOT updated"
      end
    else
      #back to previous page
      redirect_to URI.escape("/view_handler?index=0&msg=NO right to update lease item")
    end    
  end

Here is the error code from rspec:

  1) LeaseItemsController GET 'update' should render edit if update was not saved
     Failure/Error: flash[:notice].should_not be_nil
       expected: not nil
            got: nil

"Item NOT updated" was expected in flash. However why there is nothing with flash[:notice]? Or how to rspec there is a message with render 'edit', :notice => 'Item NOT updated'

Thanks.

UPDATE:

Here is change in controller:

      ...........
      else
        #back to new
        flash[:notice] = "Item NOT updated"
        render 'edit'
      end
      .........

Here is the rspec code which passes:

   it "should render edit if update was not saved" do
      item = Factory(:lease_item)
      session[:corp_head] = true
      post 'update', {:id => item.id, :lease_item => {:name => 'nil'}}
      flash.should_not be_nil
      response.should render_template(:action=> "edit")  
    end

It did not work if using flash[:notice].should_not be_nil (or .. flash.now[:notice]...). The error is Got nil which is the same as before. Also response.should render 'edit' (or ... render :action => 'edit') did not pass as well. The error is NameError or NoMethodError. Not sure why.

like image 842
user938363 Avatar asked Dec 09 '11 22:12

user938363


1 Answers

change your else to:

else
  #back to new
  flash[:notice] = "Item NOT updated"
  render 'edit'
end
like image 143
daniel Avatar answered Sep 23 '22 16:09

daniel