27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

494 CHAPTER 17 Engines<br />

Listing 17.9 spec/integration/posts_spec.rb<br />

require 'spec_helper'<br />

describe "posts" do<br />

before do<br />

@topic = Forem::Topic.new(:subject => "First topic!")<br />

@topic.posts.build(:text => "First post!")<br />

@topic.save!<br />

end<br />

end<br />

In order to reply to a topic, you’re going to need to create one. You initialize a new<br />

Forem::Topic object by using Forem::Topic.new and then build a post for it. This<br />

means that when you navigate to the topic’s page, then you’ll see a post that will have<br />

a Reply link for you to click. You’ll put the test underneath the before by using the<br />

code in the following listing.<br />

Listing 17.10 spec/integration/posts_spec.rb<br />

it "reply to a topic" do<br />

visit topics_path<br />

click_link "First topic!"<br />

within ".forem_topic #posts .forem_post" do<br />

click_link "Reply"<br />

end<br />

fill_in "Text", :with => "First reply!"<br />

click_button "Create Post"<br />

within "#flash_notice" do<br />

page.should have_content("Post has been created!")<br />

end<br />

within ".forem_topic #posts .forem_post:last" do<br />

page.should have_content("First reply!")<br />

end<br />

end<br />

In this test, you go to topics_path and click the First Topic! link. Then within a post,<br />

you click the Reply link, which will take you to the form to create a new reply to this<br />

topic. On this new page, you fill in the text with a short message and click the Create<br />

Post button to create a new post. Once this is done, you’ll be taken to the create<br />

action in Forem::PostsController, which will set the flash[:notice] to be “Post has<br />

been created!” and then redirect you back to the topic’s page, where you should see<br />

First Reply! within a post.<br />

You’ve used a slightly obscure selector syntax here; you’re looking for the<br />

#forem_topic element, which contains a #posts element, which itself contains many<br />

.forem_post elements, of which you want the last one B. This will indicate to you<br />

that on the page, the post that has been created is now at the bottom of the posts listing<br />

for this topic, right where it should be.<br />

B<br />

Find last<br />

.forem_post element

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!