Jason Kim
Jason Kim

Reputation: 19031

Why am I getting #<Capybara::Session> as the path in Rspec test?

I am trying to test the current path, and check that it is on post_path(post). I am pretty sure the test should pass. But I am getting got: #<Capybara::Session> (using ==). I don't really understand what this is.

This is the test code

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

This is the test result.

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>

Upvotes: 0

Views: 1189

Answers (1)

muttonlamb
muttonlamb

Reputation: 6501

You are running the test against page in this section

subject { page }

you need to specifically refer to the variable you are testing in this section

it { should == post_path(post) }

i.e.

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

What the test in its current for is doing is this

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

so you need to explicitly state the object you wish to test against. Capybara supports the following (depending on which version you are using)

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

or alternatively

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

Upvotes: 2

Related Questions