2013-03-19 16 views
0

我想測試當前路徑,並檢查它是否在post_path(post)。我很確定測試應該通過。但我得到got: #<Capybara::Session> (using ==)。我真的不明白這是什麼。爲什麼我在Rspec測試中獲得#<Capybara :: Session>作爲路徑?

這是測試代碼

require 'spec_helper' 

describe PostsController do 
    subject { page } 

    let(:first_user_is_admin) { FactoryGirl.create(:user) } 

    describe "Not signed in user cannot see any kind of edit view for a post:" do 

    describe "Post is anonymous without user_id" do 
     let(:post) {FactoryGirl.create(:anonymous_post)} 
     before do 
     visit edit_post_path(post) 
     end 
     it { should == post_path(post) } 
    end 

    end 
end 

這是的測試結果。

1) PostsController Not signed in user cannot see any kind of edit view for a post: Post is anonymous without user_id 
    Failure/Error: it { should == post_path(post) } 
    expected: "/posts/1" 
      got: #<Capybara::Session> (using ==) 
    Diff: 
    @@ -1,2 +1,2 @@ 
    -"/posts/1" 
    +#<Capybara::Session> 

回答

2

您正在運行對頁面的測試在本節

subject { page } 
需要特別提到您在本節

it { should == post_path(post) } 

it { variable.should == post_path(post) } 
正在測試的變量

What t他測試目前正在做的是這樣的

it { page.should == post_path(post) } 

所以你需要明確指出你想測試的對象。水豚支持以下(取決於您所使用的版本)

it { current_path.should == post_past(post) } 

或可替代

it { current_url.should == post_past(post) } 
+0

很抱歉,但我不理解這一點。你是否建議我用某些東西來替換'variable'? – 2013-03-19 07:09:25

+0

是的,我認爲在水豚是current_path.should == post_path(post) 更多關於水豚頁面的信息https://github.com/jnicklas/capybara – muttonlamb 2013-03-19 07:27:15

相關問題