2015-10-16 61 views
1

如何测试rspec请求规格中body标签中是否存在data-no-turbolink?例如,如果主页上有 <body data-no-turbolink>声明 我愿做一个测试像使用rspec测试body tag中的数据无涡轮链接

it 'home page should disable all links for turbolinks' do 
    visit '/' 
    response.should have_selector('body', data-no-turbolink) 
end 

但是上面的测试会导致语法错误。

回答

1

可以混合使用水豚匹配器在RSpec之中实际上并没有使用“访问”,使要求规范工作在大致相同的方式作为特征规格:

require 'rails_helper' 
require 'capybara/rspec/matchers' 
include Capybara::RSpecMatchers 

describe "doing stuff", type: :request do 
    it 'home page should have turbolink disabled' do 
    get "http://localhost:3000/home/index" 
    expect(response.body).to have_selector('body[data-no-turbolink]') 
    end 
end 
1

为特征规格:

# spec/features/stuff_spec.rb 
require 'rails_helper' 

describe "doing stuff", type: :feature do 
    it 'home page should have turbolink disabled' do 
    visit '/' 
    expect(page).to have_selector('body[data-no-turbolink]') 
    end 
end 
+0

这很有用,所以我已经提高了效率,但仍然希望有人能够为请求规范提供相应的答案。 – Obromios

+0

请求规范应该以相同的方式工作。尝试将'type :::feature'更改为'type :::request'并参阅。 – zetetic

+0

@zetetic:我认为这与功能之间的[差异](https://github.com/rspec/rspec-rails/blob/master/Capybara.md#user-content-upgrading-to- capybara-20)有关并请求规格。 –