2011-12-09 37 views
6

Đâ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.

+0

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

+0

Cách thiết lập flash [: notice]? – user938363

+0

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

Trả lời

5

thay đổi của bạn khác để:

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

rspec vẫn trả về lỗi: Lỗi/Lỗi: flash [: notice] .should_not be_nil mong đợi: không nil nhận: nil – user938363

+0

Thật sao? làm response.should render 'chỉnh sửa' và sau đó làm flash [: thông báo] .should_not be_nil. – daniel

+0

Lỗi có thể do chế độ xem 'chỉnh sửa' chưa được triển khai ... – user938363

0

Trước tiên, tôi không thể biết chính xác nơi bạn đặt flash trong mã của bạn.

Dù, thay thế:

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

với:

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

: thông báo được bao gồm trong lệnh kết xuất. Nó kết thúc bằng flash, phải không? – user938363

+0

Không có lỗi với cả hai lệnh bài. Có sự khác biệt thực sự nào giữa hai bài đăng đó không? – user938363

+0

: thông báo không có trong lệnh kết xuất. Render cũng không phải là một lệnh ... xem https://github.com/rails/rails/blob/d5fd83f4c7f01cc1761783408267cce4d5da1946/actionpack/lib/action_view/render/rendering.rb#L16 – daniel

Các vấn đề liên quan