Flash [: notice] .should_not be_nil failed to execute in rspec

Here is the rspec code for the 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 

Update in the controller:

  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 the flash. However, why is there nothing with flash [: notice]? Or, as rspec there is a message with the 'edit' renderer: notice => 'Item NOT updated'

Thanks.

UPDATE:

Here is the change in the controller:

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

Here is the rspec code that 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 

This did not work if you use flash [: notice] .should_not be_nil (or .. flash.now [: notice] ...). Error: Got nil, which is similar to the previous one. Also, response.should render 'edit' (or ... render: action => 'edit') also failed. Error: NameError or NoMethodError. I do not know why.

+6
source share
2 answers

change your value to:

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

Firstly, I don’t see where exactly you installed flash in your code.

Whatever happens, replace:

  post 'update', {:id => item.id, :name => 'nil'} 

from:

  post 'update', {:id => item.id, :lease_item => { :name => 'nil' } } 
0
source

Source: https://habr.com/ru/post/903489/


All Articles