簡體   English   中英

have_selector正在測試失敗

[英]have_selector is failing test

我有一個問題,包括rspec的a have_selector問題:

這是我的代碼:

describe "GET 'home'" do
  it "returns http success" do
    get 'home'
    expect(response).to be_success
  end

  it "should have the right title" do
    should have_selector("title", 
          :content => "Ruby on Rails Tutorial Sample App | Home")
  end
end

我在頂部包括以下內容:

RSpec.describe PagesController, :type => :controller do
  render_views

我的html5有以下內容:

<title>Ruby on Rails Tutorial Sample App | Home</title>

我收到一條錯誤消息:

失敗:

1) PagesController GET 'home' should have the right title
 Failure/Error: should have_selector("title",
   expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
 # ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top  (required)>

有人可以幫忙嗎?

rspec -v 3.0.2

軌道4.1.1

先感謝您。

默認情況下,Rspec 3不包括控制器規格中的水豚匹配器。 您可以通過執行此操作為單個規格更改此設置

include Capybara::RSpecMatchers

或者,在您的規范助手中

  config.include Capybara::RSpecMatchers, :type => :controller

您的下一個問題是,最近版本的水豚不允許您默認測試不可見元素的存在,並且標題元素被認為是不可見的。 您應該使用have_title匹配器。

Hollo ruby​​ist朋友!

我有這個問題,那是因為有兩件事,首先我沒有使用capybara gem,第二個have_selector只接受以下之一:count,:minimum,:maximum,:between,:text,:visible,:exact,:match ,:等待鍵並不明白:內容。

我相信你已經解決了這個問題,但是對於那些最近開始學習Ruby on Rails並且遇到這樣一個問題的人我應該說要首先將它放入你的Gemfile中,如下所示:

group :development, :test do
  ...
  gem 'capybara'
end

並運行命令

 bundle install 

然后在pages_controller_spec.rb文件中測試你的視圖是否有特定的標題

describe "GET #home" do
  ...
  it "should have the right title" do
    get :home
    expect(response.body).to have_selector('title', :text => 'Ruby on Rails rocks')
  end
end

我希望它會對某人有所幫助。

Cheerio!

檢查內容是視圖規范...而控制器規范意味着檢查響應狀態/路徑,渲染頁面/部分,實例變量過濾器參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM