Reputation: 10350
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.
Upvotes: 6
Views: 16826
Reputation: 9835
change your else to:
else
#back to new
flash[:notice] = "Item NOT updated"
render 'edit'
end
Upvotes: 5
Reputation: 115531
First, I can't see where exactly you set the flash
in your code.
Whatever, replace:
post 'update', {:id => item.id, :name => 'nil'}
with:
post 'update', {:id => item.id, :lease_item => { :name => 'nil' } }
Upvotes: 0