Skip to content

Instantly share code, notes, and snippets.

@kidach1
Created November 25, 2013 03:57
Show Gist options
  • Save kidach1/7636139 to your computer and use it in GitHub Desktop.
Save kidach1/7636139 to your computer and use it in GitHub Desktop.
[Rails] RSpecのリファクタリング ref: http://qiita.com/kidachi_/items/ec184deb106e4e5c90c3
module ApplicationHelper
# ページごとの完全なタイトルを返却
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
expect(full_title("foo")).to match(/foo/)
end
it "should include the base title" do
expect(full_title("foo")).to match(/^Ruby on Rails Tutorial Sample App/)
end
it "should not include a bar for the home page" do
expect(full_title("")).not_to match(/\|/)
end
end
end
describe "Static pages" do
describe "Home page" do
end
end
it "should have the h1 'Sample App'" do
visit root_path
page.should have_selector('h1', text: 'Sample App')
end
let(:variable) { 'hoge' }
# 値’hoge’を持つ変数variableが使えるようになる。
describe "Static pages" do
subject { page }
shared_examples_for "fuga" do
it { should have_content(variable1) }
it { should have_title(full_title(variable2)) }
end
~
describe "Home page" do
let(:variable1) { 'Sample App' }
let(:variable2) { '' }
it_should_behave_like "fuga"
end
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_content(heading) }
it { should have_title(full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_title('| Home') }
end
describe "About page" do
before { visit about_path }
let(:heading) { 'About' }
let(:page_title) { 'About' }
it_should_behave_like "all static pages"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment