且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 shared_examples_for 的正确方法

更新时间:2022-12-29 16:47:43

不确定您的问题究竟是什么,但共享示例中的 describe 块有多大必要?那是我第一次刺伤.

Not sure what your issue is really, but how necessary is the describe block in the shared example? That's my first stab.

此代码对我有用.

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