为什么我将#<capybara :: session>作为Rspec测试中的路径?</capybara :: session>

时间:2013-03-19 04:39:32

标签: ruby-on-rails rspec capybara

我正在尝试测试当前路径,并检查它是否在post_path(post)上。我很确定测试应该通过。但我得到了got: #<Capybara::Session> (using ==)。我真的不明白这是什么。

这是测试代码

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>

1 个答案:

答案 0 :(得分:2)

您正在本节中针对页面运行测试

subject { page }

您需要专门参考本节中正在测试的变量

it { should == post_path(post) }

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

目前正在进行的测试是

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

因此您需要明确说明要测试的对象。 Capybara支持以下内容(取决于您使用的版本)

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

或者

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