使用rspec测试body标签中的data-no-turbolink

时间:2015-10-16 01:12:48

标签: ruby-on-rails rspec

如何在rspec请求规范中测试body标签中是否存在data-no-turbolink?例如,如果主页有 <body data-no-turbolink>声明 我想做一个像

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

但是,上述测试会导致语法错误。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您可以将Capybara匹配器混合到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