2012-12-27 170 views
1

我遇到了rspec的一些問題,它會返回一個錯誤,即使所有東西都在localhost上工作。未定義的方法`node_name'爲零:NilClass

1) Item pages item creation with invalid information should not create an item 
    Failure/Error: expect { click_button "Add" }.not_to change(Item, :count) 
    NoMethodError: 
     undefined method `node_name' for nil:NilClass 
    # (eval):2:in `click_button' 
    # ./spec/requests/item_pages_spec.rb:20:in `block (5 levels) in <top (required)>' 
    # ./spec/requests/item_pages_spec.rb:20:in `block (4 levels) in <top (required)>' 

item_pages_spec.rb

require 'spec_helper' 

describe "Item pages" do 

    subject { page } 

    let(:user) { FactoryGirl.create(:user) } 
    let(:list) { FactoryGirl.create(:list, user: user)} 
    before { sign_in user } 

    describe "item creation" do 
    before do 
     visit user_list_path(user, list) 
     click_link "Add wish" 
    end 

    describe "with invalid information" do 

     it "should not create an item" do 
     expect { click_button "Add" }.not_to change(Item, :count) 
     end 

     # describe "error messages" do 
     # before { click_button "Add" } 
     # it { should have_content('error') } 
     # end 
    end 

    describe "with valid information" do 

     before { fill_in 'item_title', with: "Lorem ipsum" } 
     it "should create a item" do 
     expect { click_button "Add" }.to change(Item, :count).by(1) 
     end 
    end 
    end 
end 

形式創建一個項目

<% if signed_in? %> 

    <div id="addWish" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="addWishLabel" aria-hidden="true"> 
    <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
     <h3 id="addWishLabel">Add wish</h3> 
    </div> 
    <div class="modal-body"> 
     <%= form_for(@item) do |f| %> 
     <%= render 'shared/error_messages', object: f.object %> 
     <div class="field"> 
     <%= f.hidden_field :list_id, value: @list.id %> 
     <%= f.text_field :title, placeholder: "Title..." %> 
     <%= f.text_field :link, placeholder: "Link..." %> 
     </div> 
    </div> 
    <div class="modal-footer"> 
     <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> 
     <%= f.submit "Add", class: "btn btn-primary" %> 
     <% end %> 
    </div> 
    </div> 
<% end %> 

我很新的軌道和任何幫助因而非常感謝!

+0

你是什麼意思,即使「一切工作在本地主機」。 你是否試圖在不同的主機上運行不同的Capybara驅動程序測試? –

+1

不好意思,因爲缺乏清晰度。我的意思是,當我通過瀏覽器測試時,一切正常。 –

回答

2

必須將click_button "Add"更改爲click_link "Add"

+1

真的嗎?將'click_button'改爲'click_link'對我來說不起作用。對我來說,理順我的結束標籤的順序解決了這個問題,我在[這篇文章]中看到了這個問題(http://stackoverflow.com/questions/10613354/how-do-i-click-this-button-水豚),它看起來像你的標籤也可能失靈。 – lucas

0
expect { click_button "Add" }.to_not change(Item.count) 

expect { click_button "Add" }.to_not change(@items, :count) 
+0

這會產生錯誤'TypeError:nil不是符號' –

0

如果有其他人落在這裏,請檢查您的html標記爲@lucas提到的。這可能是你的情況。我發現這個here。 如果您的標籤嚴重關閉,水豚將無法正確找到您的按鈕。 就我而言,我有這樣的:

<div> 
    <%= form_tag do %> 
     <%= submit_tag %> 
    </div> 
    <% end %> 

關閉div中的form_tag外爲我做。

相關問題