Reputation: 19031
I might have a flawed understanding of what shared_examples_for
should do, but hear me out.
Basically, I have a common navigation bar that appears in index
page and new
page of Forum. So I want tests for navigation bar to perform for both index
page and new
page. I was hoping that code below using shared_examples_for
would accomplish that. But what happened was that, test cases in shared_examples_for
simply is not running. To check I created failing test case within the shared_examples_for
scope, but the tests didn't fail.
What am I doing wrong?
require 'spec_helper'
describe "Forums" do
subject { page }
shared_examples_for "all forum pages" do
describe "should have navigation header" do
it { should have_selector('nav ul li a', text:'Home') }
it { should have_selector('nav ul li a', text:'About') }
end
end
describe "Index forum page" do
before { visit root_path }
...
end
describe "New forum page" do
before { visit new_forum_path }
...
end
end
Upvotes: 4
Views: 8838
Reputation: 9000
Here's a nice intention-revealing way to bind these things together:
shared_examples_for 'a page with' do |elements|
# the following two would be navs for a page with
it { should have_selector 'h1', text: 'About' }
it { should have_selector 'a', text: 'Songs' }
# these would be dynamic depending on the page
it { should have_selector('h1', text: elements[:header]) }
it { should have_selector('title', text: full_title(elements[:title])) }
end
describe "About" do
it_behaves_like 'a page with', title: 'About', header: 'About Header' do
before { visit about_path }
end
end
describe "Songs" do
it_behaves_like 'a page with', title: 'Songs', header: 'Songs Header' do
before { visit songs_path }
end
end
Upvotes: 12
Reputation: 875
Not sure what your issue is really, but how necessary is the describe block in the shared example? That's my first stab.
This code works for me.
shared_examples_for 'all pages' do
# the following two would be navs for all pages
it { should have_selector 'h1', text: 'About' }
it { should have_selector 'a', text: 'Songs' }
# these would be dynamic depending on the page
it { should have_selector('h1', text: header) }
it { should have_selector('title', text: full_title(title)) }
end
describe "About" do
before { visit about_path }
let(:title) {'About'}
let(:header) {'About Site'}
it_should_behave_like 'all pages'
end
describe "Songs" do
before { visit songs_path }
let(:title) { 'Songs Title' }
let(:header) { 'Songs' }
it_should_behave_like 'all pages'
end
Upvotes: 7