使用 shared_examples_for 的正确方法 [英] Correct way to use shared_examples_for

查看:50
本文介绍了使用 shared_examples_for 的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能对 shared_examples_for 应该做什么有一个有缺陷的理解,但请听我说.

I might have a flawed understanding of what shared_examples_for should do, but hear me out.

基本上,我有一个通用的导航栏,出现在论坛的index 页面和new 页面中.所以我希望对 index 页面和 new 页面执行导航栏测试.我希望下面使用 shared_examples_for 的代码可以实现这一点.但实际情况是,shared_examples_for 中的测试用例根本没有运行.为了检查我在 shared_examples_for 范围内创建了失败的测试用例,但测试没有失败.

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.

我做错了什么?

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

推荐答案

不确定您的问题究竟是什么,但共享示例中的 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

这篇关于使用 shared_examples_for 的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆