Correct way to use shared_examples_for

前端 未结 2 949
予麋鹿
予麋鹿 2021-01-19 04:44

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

2条回答
  •  一向
    一向 (楼主)
    2021-01-19 04:53

    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
    

提交回复
热议问题