ruby - RSpec - 跨路线测试多个期望

标签 ruby testing rspec sinatra capybara

我正在尝试使用 shared_examples 作为跨多条路线重复期望的方式。特别是,我想测试页眉和页脚中的一些静态资源是否正在加载。但是,我收到一条错误消息:

RSpec::Core::ExampleGroup::WrongScopeError: `it_behaves_like` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

现在,我不确定如何解决这个问题。这是我当前的设置。

shared_examples_for 'a page' do 
    describe 'should load static assets' do 
        it 'header, footer and icons' do 
            expect(page).to have_css 'footer.footer'                
            expect(page).to have_css '#navbar-brand'                
            brand = page.first(:css, '#navbar-brand')
            visit brand[:src]                                       
            expect(page.status_code).to be 200 
        end
    end
end


describe 'site pages should load static assets on requests', { :type => :feature } do 

after :all do 
    it_behaves_like 'a page'
end

it 'home page' do
    visit '/'
    expect(page).to have_css 'div#main-menu a', count: 5 
    page.all('link[rel~="icon"]').each do |fav|             
        visit fav[:href]
        page.status_code.should be 200
    end 
    expect(page).to have_css 'div#main-menu'
    ...     
end

it 'about page should have icons, footer and a header' do 
    visit '/about'
    ...
end 

结束

另一个尝试是这样的:

describe 'home page' do 
    it_behaves_like 'a page'
end 

两者都因上述相同原因而失败。那么,如果我想在每个页面上检查相同的内容,什么是更好的解决方案?

最佳答案

RSpec 3这应该有效

require 'spec_helper'

shared_examples 'a page' do 
  it 'has a header' do
    expect(page).to have_css 'footer.footer'
  end

  it 'has a brand' do       
    expect(page).to have_css '#navbar-brand'
  end

  it 'points out to brand page' do
    brand = page.first(:css, '#navbar-brand')
    visit brand[:src]                                      
    expect(page.status_code).to be 200 
  end
end

describe 'home page' do
  before { visit '/' }      

  it_behaves_like 'a page'

  it 'does something else' do
    # ...
  end
end

或者你可以使用一个 block

describe 'home page' do
  it_behaves_like 'a page' do
    before { visit '/' }
  end
end

关于ruby - RSpec - 跨路线测试多个期望,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30810384/

相关文章:

testing - 将 QTP 结果导出到 html 报告

ruby-on-rails - RSpec 无法在模块内找到类

ruby-on-rails - rails 路由和参数,其中包含 '.'

ruby-on-rails - 使用 state_machine gem,有没有办法将事件设为私有(private)/ protected ?

ruby-on-rails - Rails,将整数转换为数组偏移量

c# - 为什么 C# ProcessStartInfoRedirectStandardOutput 会导致 xcopy 进程失败

haskell - 使用类型类为使用 Acid-State 时提供替代实现

ruby-on-rails - 静态设置 : error with Compass integration

java - 在phpbb3中获取form_token和time_creation

rspec - 如何在 puppet-rspec 测试中设置 puppet 环境?