Correct way to use shared_examples_for

前端 未结 2 948
予麋鹿
予麋鹿 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 05:07

    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
    

提交回复
热议问题