0

I've already added the reply-to-microposts functionality to Michael Hartl's Rails 3 Tutorial book, but I'm now trying to write the tests for them (I know I did it backwards). The replies addition works, but I'm trying to write the following test

  1. create user and two posts from user
  2. create other_user and two posts from other_user
  3. have user follow other_user
  4. check that user's home page feed includes other_user's post
  5. ...

My tests are currently failing at step four. Below is the snippet from the spec file, the failure error, and the link to my repo. (user is defined before this describe, but within the scope of this block)

describe "replying to a micropost" do
    let (:other_user) { FactoryGirl.create(:user) }
    let (:first_post) { FactoryGirl.create(:micropost, user: other_user, content: "Whatever.") }
    let (:second_post) { FactoryGirl.create(:micropost, user: other_user, content: "Nevermind.") }

    before do
        user.follow!(other_user)
        visit root_path
    end

    it "should render the posts from other user" do
        page.should have_selector("li##{first_post.id}", text: first_post.content)
        page.should have_selector("li##{second_post.id}", text: second_post.content)
    end


Failures:

  1) Static pages Home page for signed-in users replying to a micropost should render the posts from other user
     Failure/Error: page.should have_selector("li##{first_post.id}", text: first_post.content)
       expected css "li#203" with text "Whatever." to return something
     # ./spec/requests/static_pages_spec.rb:85:in `block (5 levels) in <top (required)>'

Finished in 3.43 seconds
17 examples, 1 failure

Failed examples:

rspec ./spec/requests/static_pages_spec.rb:84 # Static pages Home page for signed-in users replying to a micropost should render the posts from other user

Done.

https://github.com/johnklawlor/sample_app

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
johnklawlor
  • 1,708
  • 2
  • 13
  • 15
  • Please share the specific failure you're getting. – Peter Alfvin Jan 03 '14 at 19:30
  • just added the rspec failures... – johnklawlor Jan 03 '14 at 19:36
  • Sorry, but don't see anything obvious and not to up combing through your source repository. :-) This is a good case for why you want to TDD with small chunks, but given where you are, you might want to start with printing out the current page content at the point of failure. – Peter Alfvin Jan 03 '14 at 19:42
  • how do i print out the current page content at the point of failure? – johnklawlor Jan 03 '14 at 19:50
  • Actually, try http://jeffkreeftmeijer.com/2010/ever-heard-of-capybaras-save_and_open_page-method/ first – Peter Alfvin Jan 03 '14 at 20:01
  • I think I've figured it out. – johnklawlor Jan 04 '14 at 06:24
  • I think it's related to a statement iterated in this question regarding let vs before initializations, where s/he states, "For the method defined by let, the initialization code only runs if the example calls it." (source: http://stackoverflow.com/questions/5359558/when-to-use-rspec-let) In my example, first_post and second_post weren't being created until I called on their id's in the have_selector('li'... call, which means they aren't created until *after* the visit root_path call and therefore after the user's feed is built. The only thing I have to back up... – johnklawlor Jan 04 '14 at 06:30
  • ...this assumption is that if you add some line like first_post.content=first_post.content (and the same for second_post) *before* the visit root_path call, the
  • tags are generated for the posts as attested by the page opened from save_and_open_page and by the passing tests. Inserting a line like first_post.content=first_post.content after visit root_path produces no li tags and failing tests.
  • – johnklawlor Jan 04 '14 at 06:34
  • AKA let! Use let! instead of let, is the moral of this 8-hour story. – johnklawlor Jan 04 '14 at 06:37
  • I can't believe I didn't catch this myself. You should post your explanation as an answer. This `let` pitfall has been discussed countless times on SO, but it can't be reinforced too often imho. – Peter Alfvin Jan 04 '14 at 20:23
  • thanks--i didn't know i could answer my own question. – johnklawlor Jan 05 '14 at 20:30