2013-04-21 57 views
3

當我在本地主機上以開發模式運行時。一切正常,我可以輕鬆刪除我的帖子。但是我的請求規範失敗了。無是Rspec中的符號

我得到這個錯誤,當我運行我的測試:

1) Posts when logged in should delete posts 
    Failure/Error: expect{click_on "Usuń spot"}.to change(Post.count).by(1) 
    TypeError: 
     nil is not a symbol 

posts_spec.rb

describe "when logged in" do 
    let(:user) {FactoryGirl.create(:user)} 
    let!(:post) {user.posts.create(content: "Brilliant! I just saw the most amazing ever. She looked so cute!")} 
    content = "Example of spot post, for TDD. It's not real spot. Not yet." 

    before(:each) { 
     log_user(user) 
    } 

    it "should delete posts" do 
     visit user_post_path(user, post) 
     expect{click_on "Usuń spot"}.to change(Post.count).by(1) 
    end 

show.haml

[email protected] 
=link_to "Usuń spot", [@user,@post], method: :delete 

posts_controller.rb

def show 
    @user = current_user 
    @post = @user.posts.find(params[:id]) 
    end 

    def destroy 
    @user = current_user 
    @post = @user.posts.find_by_id(params[:id]) 
    @post.destroy 

    flash[:success] = "Post destroyed" 

    redirect_to root_url 
    end 

回答

19

change需要一個塊,因此可以對它進行兩次評估以驗證值的變化。

這應該工作:

expect{click_on "Usuń spot"}.to change{Post.count}.by(1) 
+0

謝謝。我完全沒有看到它。我需要檢查我的眼睛,我確信我是通過它作爲塊:D – 2013-04-21 13:44:54