【发布时间】:2015-08-28 21:29:26
【问题描述】:
我正在尝试使用 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
由于上述相同的原因,两者都失败了。那么,如果我想在每个页面上检查相同的内容,有什么更好的解决方案?
【问题讨论】:
标签: ruby testing rspec sinatra capybara