今日は朝練の日 (2)
Chapter 3 をざくっと見てみると最後に Exercise なるソレがあるみたい。ので 3.3 も引き続き見てみます。
ええと title な中身の試験をするために have_selector というメソドを使うとのこと。以下がサンプル。
it "should have the right title" do get 'home' response.should have_selector("title", :content => "Ruby on Rails Tutorial Sample App | Home") end
ちなみに上記試験は red なはず。たとえば application.html.erb の title なナニが以下だし。
<!DOCTYPE html> <html> <head> <title>SampleApp</title>
とりあえず試験を盛り込んで実行。
$ bundle exec rspec spec/ .F.F.F Failures: 1) PagesController GET 'home' should have the right title Failure/Error: response.should have_selector("title", expected following output to contain a <title>Ruby on Rails Tutorial Sample App | Home</title> tag: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 2) PagesController GET 'contact' should have the right title Failure/Error: response.should have_selector("title", expected following output to contain a <title>Ruby on Rails Tutorial Sample App | Contact</title> tag: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> # ./spec/controllers/pages_controller_spec.rb:27:in `block (3 levels) in <top (required)>' 3) PagesController GET 'about' should have the right title Failure/Error: response.should have_selector("title", expected following output to contain a <title>Ruby on Rails Tutorial Sample App | About</title> tag: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> # ./spec/controllers/pages_controller_spec.rb:41:in `block (3 levels) in <top (required)>' Finished in 5.3 seconds 6 examples, 3 failures Failed examples: rspec ./spec/controllers/pages_controller_spec.rb:11 # PagesController GET 'home' should have the right title rspec ./spec/controllers/pages_controller_spec.rb:25 # PagesController GET 'contact' should have the right title rspec ./spec/controllers/pages_controller_spec.rb:39 # PagesController GET 'about' should have the right title $
うをを。ここまで文句言われると直さざるを得ないですね。対処としては
- app/views/layouts/application.html.erb 削除
- app/views/pages/about.html.erb 修正
- app/views/pages/contact.html.erb 修正
- app/views/pages/home.html.erb 修正
盛り込んで試験。パスしない。原因不明。
買い物より帰還してきんぴら作成後
_Note that the render_views line introduced in Listing 3.15 is necessary for the title tests to work._という記述を見つけて spec/controllers/pages_controller_spec.rb に以下を盛り込んだ後に
describe PagesController do render_views
試験実施。
$ bundle exec rspec spec/ ...... Finished in 1.9 seconds 6 examples, 0 failures $
green に。やれやれ。
Instance variables and Embedded Ruby
ええと @title って属性作って云々。app/controllers/pages_controller.rb を以下に修正して
class PagesController < ApplicationController def home @title = "Home" end def contact @title = "Contact" end def about @title = "About" end end
app/views/pages/ 配下のソレを例えば home.html.erb だと以下に修正。
<!DOCTYPE html> <html> <head> <title>Ruby on Rails Tutorial Sample App | <%= @title %></title> </head> <body> <h1>Sample App</h1> <p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application. </p> </body> </html>
このあたり、説明の文章ほぼスルーなんだけど大丈夫かな。とりあえず home.html.erb だけ修正して試験 green なので、他も修正して試験 green を確認。
この状態だと後は body の中身が微妙に違うだけなので、app/views/layout/application.html.erb に共通部分を括り込んで差分だけを views/pages に持っておけば良い。
Eliminating duplication with layouts
盛り込んでみます。まず app/views/layout/application.html.erb が以下。
<!DOCTYPE html> <html> <head> <title>Ruby on Rails Tutorial Sample App | <%= @title %></title> <%= csrf_meta_tag %> </head> <body> <%= yield %> </body> </html>
で、app/views/pages/home.html.erb が以下。
<h1>Sample App</h1> <p> This is the home page for the <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> sample application. </p>
他も同様に修正。試験 green を確認。
Conclusion
git commit しとけ的記述あり。
$ git add . $ git commit -m "Done with static pages"
とは言え、status 確認してみました。
$ git status # On branch static-pages # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: app/views/layouts/application.html.erb # modified: config/routes.rb # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # Gemfile.lock # app/controllers/pages_controller.rb # app/controllers/pages_controller.rb~ # app/helpers/pages_helper.rb # app/views/pages/ # spec/controllers/ no changes added to commit (use "git add" and/or "git commit -a") $
ええと、.gitignore に以下を追加。
*~ Gemfile.lock
で、以下。
$ git add . $ git commit -m "Done with static pages" $ git checkout master $ git merge static-pages
あ、--no-ff するの忘れたorz
Exercises
面白そう。こっちをヤるか linaro な掘削をするかは不明なのでここでエントリ投入します。