27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

504 CHAPTER 17 Engines<br />

You don’t need this controller to do much more than sit there and look pretty. Oh,<br />

and it needs to have a login action that responds in an OK fashion too. Let’s define<br />

this controller in your dummy application by creating a new file at spec/dummy/app/<br />

controllers/fake_controller.rb and putting this content inside it:<br />

class FakeController < ApplicationController<br />

def sign_in<br />

render :text => "Placeholder login page."<br />

end<br />

end<br />

This action will now render the text “Placeholder login page,” thereby returning that<br />

OK status you’re after, as well as some helpful text to indicate where you’re at. When<br />

you run bin/rspec spec/integration/topics_spec.rb, you’ll see that it passes:<br />

1 example, 0 failures<br />

This means now that any user attempting to access the new action in the<br />

Forem::TopicsController will be redirected to the login page. What happens when<br />

you run the whole spec file? Let’s find out with bin/rspec spec/integration/<br />

topics_spec.rb:<br />

ActionView::Template::Error:<br />

undefined method `current_user' for #<br />

...<br />

# ./spec/integration/topics_spec.rb:21:in ...<br />

3 examples, 1 failure<br />

Your final spec in this file is failing with an undefined method current_user, because<br />

you’re not calling sign_in! before it. Move this code into its own context with a<br />

before like the other two tests have, using the code shown in the following listing.<br />

Listing 17.15 spec/integration/topics_spec.rb<br />

context "authenticated users" do<br />

before do<br />

sign_in!<br />

end<br />

it "creating a new one" do<br />

visit topics_path<br />

click_link "New Topic"<br />

fill_in "Subject", :with => "First topic!"<br />

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

click_button "Create Topic"<br />

within "#flash_notice" do<br />

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

end<br />

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

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

end<br />

end<br />

end

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

Saved successfully!

Ooh no, something went wrong!