0

我正在尝试测试当前路径,并检查它是否打开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>
4

1 回答 1

2

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

subject { page }

您需要在本节中专门引用您正在测试的变量

it { should == post_path(post) }

IE

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) }
于 2013-03-19T05:13:24.570 回答