0

I have been struggling to using ruby/rspec/capybara/devise to test my code. A simple test I am trying to write is for signing in. I have a valid user sign in and expect to see an h1 tag as defined in the following code:

describe "Authentication" do

  subject { page }
  describe "with valid information" do
    let(:user) { FactoryGirl.create(:user) }
    before { sign_in_with user.email }

    it { should have_css('h1', text: "Welcome to the Test") }
  end
end

Problem is that I get this in return:

1) Authentication signin page with valid information 
 Failure/Error: it { should have_css('h1', text: "Welcome to the Test") }
   expected css "h1" with text "Welcome to the Test" to return something
 # ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

Is there a way to output what the test found in the h1 (or that it didn't find it at all?) instead of it didn't find what it expected? There is a good chance my sign_in method is not working, but I can't validate that because I'm not sure what the test sees after sign_in_with executes.

Thanks and happy to provide more context if it's helpful.

EDIT Updated code to reflect subject of tests.

Ryan
  • 641
  • 5
  • 17

2 Answers2

1

... I'm not sure what the test sees after sign_in_with executes.

You can open a snapshot of the current page with save_and_open_page:

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before { sign_in_with user.email }

  it { save_and_open_page; should have_css('h1', text: "Welcome to the Test") }
end
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • Only caveat I ran into was that `save_and_open_page` needed to be in the it statement: http://stackoverflow.com/questions/12608976/save-and-open-page-capybara-launchy-stopped-working-in-a-project-error – Ryan Apr 08 '13 at 17:12
0

There is no subject, you can't use it to represent result. For Capybrara, you need to check the page object returned.

Let's rewrite the test like this:

describe "with valid information" do
  let(:user) { FactoryGirl.create(:user) }
  before do
    sign_in_with user.email
  end
  it "signs in successfully" do
    expect(page).to have_css('h1', text: "Welcome to the Test")
  end        
end

Or better, with Capybara story DSL

feature "User sign in" do

  given(:user) { FactoryGirl.create(:user) }

  scenario "with valid information " do
    sign_in_with user.email
    expect(page).to have_css('h1', text: "Welcome to the Test")
  end        

  scenario "with invalid filing" do
    sign_in_with "foo@bar.com"
    expect(page).to have_text("Invalid email or password")
  end
end
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • Sorry that it wasn't clear, but the page is set as the subject of the tests. I have updated the code to include that. But my question isn't so much why my test is failing so much as how do I understand what the test is seeing as the page rather than it simply did not see the expected element. – Ryan Apr 08 '13 at 16:57
  • I think your `subject` need to put within the `describe` block and under `before`. Besides, I don't prefer to use `subject` in integration tests, the style is going to use lots of short `it` blocks which will be very heavy in integration tests. Also, the short `it` block doesn't fit the integration tests well, which tends to have several steps normally. – Billy Chan Apr 08 '13 at 17:02
  • For your question, that's about Capybara's design which I don't quite know. But I think the logic is quite understandable. There are many actions, click link, click button, hover, scroll etc, you can't inspect the actions themselves, you can only inspect the side effect they brought -- the page. – Billy Chan Apr 08 '13 at 17:08