簡體   English   中英

Capybara和Rspec。 Capybara在Test中沒有渲染頁面,在rails中使用visit或get方法

[英]Capybara and Rspec. Capybara not Rendering page in Test, with visit or get method in rails

我是初學者,學習Ruby on Rails,我有一個問題讓我的測試工作。 我使用rspec和水豚。 問題是Capybara沒有渲染頁面或它沒有到達正確的頁面。 執行時,頁面在瀏覽器中正確打開:$ rails s。 但是當我使用save_and_open_page進行測試時,html是空的。

我的規格

以下是我的第一個PagesController規范:

 require 'spec_helper'

describe PagesController do
describe "GET 'home'" do
    it "returns http success" do
      visit pages_home_path
      save_and_open_page #at this point it opens the a blank html page
    end
  end
end

我的控制器規范,這將打開一個空白頁面。

我嘗試了Pagescontroller規范的替代語法:

require 'spec_helper'

describe PagesController do

  describe "GET 'home'" do
    it "returns http success" do
       get :home           
       response.should render_template('home')
       response.should have_content("home") #it fails here
     end
  end
end

這會在控制台中顯示以下錯誤:

1)PagesController GET'home'返回http成功失敗/錯誤:response.should have_content(“home”)期望在“#”#./spec/controllers/pages_controller_spec.rb:10:in中有文本“home”。阻止(3級)'

完成1.91秒1例,1次失敗

其他配置:

在spec_helper中我添加了以下行,其余部分未被觸及。

require 'capybara/rspec'

* 寶石文件:*

    gem 'rails', '3.1.0'
    group :development, :test do
              gem 'autotest-rails'
      gem 'sqlite3'
      gem 'ruby-debug19', :require => 'ruby-debug'
      gem 'cucumber-rails', :require => false
      gem 'cucumber-rails-training-wheels'
      gem 'database_cleaner'
      gem 'capybara'
      gem 'launchy'
      gem 'rspec-rails'
      gem 'simplecov'
      gem 'rspec-core', '2.8.0'
    end
    group :assets do
      gem 'therubyracer'              
      gem 'sass-rails', "  ~> 3.1.0"
      gem 'coffee-rails', "~> 3.1.0"
      gem 'uglifier'
    end

    gem 'jquery-rails'
    gem 'haml'

我沒有安裝Webrat並且在我的Gem列表中驗證了它。 謝謝您的幫助。

文檔中看,您應該將您的規格標記為feature或將它們放入spec/features文件夾中。

對我來說,似乎你有控制器測試的默認行為,即渲染任何東西。 您可以覆蓋它,但我認為將集成測試與控制器測試分開會更好。

聽起來你的第二個測試是發出請求而無法找到內容,因為內容不在“響應”中,但在“response.body”中,請嘗試這樣:

it "returns http success" do
   get :home           
   response.should render_template('home')
   response.body.should have_content("home") 
 end

暫無
暫無

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

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