2015-10-16 41 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)有關並請求規格。 –