Đây là mã rspec cho điều khiển:đèn flash [: Thông báo] .should_not be_nil thất bại trong rspec
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
Bản cập nhật trong điều khiển là:
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
Đây là mã lỗi từ 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
"Mục không được cập nhật" được mong đợi trong flash. Tuy nhiên tại sao không có gì với flash [: notice]? Hoặc làm thế nào để rspec có một thông báo với render 'edit',: notice => 'Item NOT updated'
Cảm ơn.
UPDATE:
Đây là sự thay đổi trong điều khiển:
...........
else
#back to new
flash[:notice] = "Item NOT updated"
render 'edit'
end
.........
Đây là mã rspec mà đi:
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
Nó không làm việc nếu sử dụng đèn flash [: Thông báo ] .should_not be_nil (hoặc .. flash.now [: notice] ...). Lỗi là Got nil giống như trước. Ngoài ra response.should render 'edit' (hoặc ... render: action => 'edit') cũng không được chuyển. Lỗi là NameError hoặc NoMethodError. Không chắc chắn lý do tại sao.
Bạn đang gửi "Item KHÔNG cập nhật" trong biến 'notice' với phần 'edit', nhưng bạn có thực sự thiết lập' flash [: notice] '? – Karl
Cách thiết lập flash [: notice]? – user938363
không có hiển thị 'foo',: notice => "blah" ... đọc tài liệu. Sử dụng flash [: notice] = "Item NOT updated" sau đó render 'edit' – daniel