2013-11-25 34 views
0

我非常喜歡水豚和rpsec,我可以用它們寫一些很棒的集成測試。但是,我有些困惑。我可能是錯在這裏,但現在看來,一旦我安裝了水豚,我有我的規格在spec/features,水豚的方法,如visit可供規格內spec/features(如spec/features/controllers/statuses_spec.rb), rspec的方法,如route_to現在無法使用這些規格!使用水豚和rspec時沒有方法錯誤

這是什麼意思?水豚是否提供彌補我現在不可用的rspec方法的方法?例如,visit,而不是get

這看起來不太直觀。我希望我做的關於我的設置一些錯誤:

的Gemfile:

source 'https://rubygems.org' 

group :development do 
    gem 'capistrano' 
    gem 'guard-rspec' 
    gem 'rb-fsevent' 
    gem 'debugger' 
end 

group :development, :test do 
    gem 'rspec-rails', '~> 2.14.0' 
    gem 'sqlite3' 
end 

group :test do 
    gem 'factory_girl_rails' 
    gem 'capybara', '~> 2.2.0' 
# gem "capybara-webkit" 
    gem 'launchy' 
    gem 'database_cleaner' 
end 

group :production do 
    gem 'pg' 
end 

gem 'rails', '4.0.1' 
gem 'sass-rails', '~> 4.0.0' 
gem 'uglifier', '>= 1.3.0' 
gem 'coffee-rails', '~> 4.0.0' 
gem 'jquery-rails' 
gem 'turbolinks' 
gem 'jbuilder', '~> 1.2' 

group :doc do 
    gem 'sdoc', require: false 
end 

gem 'devise' 
# Use puma as the app server 
# gem 'puma' 

規格/ spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' 
ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'rspec/autorun' 
require 'capybara/rspec' 

# Requires supporting ruby files with custom matchers and macros, etc, 
# in spec/support/ and its subdirectories. 
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 

# Checks for pending migrations before tests are run. 
# If you are not using ActiveRecord, you can remove this line. 
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) 

RSpec.configure do |config| 
    # ## Mock Framework 
    # 
    # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: 
    # 
    # config.mock_with :mocha 
    # config.mock_with :flexmock 
    # config.mock_with :rr 
    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

    config.before(:each) do 
    Capybara.run_server = true 
    Capybara.javascript_driver = :webkit 
    Capybara.default_selector = :css 
    Capybara.server_port = 7171 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 
    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    config.include RSpec::Rails::RequestExampleGroup, type: :feature 

    # If you're not using ActiveRecord, or you'd prefer not to run each of your 
    # examples within a transaction, remove the following line or assign false 
    # instead of true. 
    config.use_transactional_fixtures = true 

    # If true, the base class of anonymous controllers will be inferred 
    # automatically. This will be the default behavior in future versions of 
    # rspec-rails. 
    config.infer_base_class_for_anonymous_controllers = false 

    # Run specs in random order to surface order dependencies. If you find an 
    # order dependency and want to debug it, you can fix the order by providing 
    # the seed, which is printed after each run. 
    #  --seed 1234 
    config.order = "random" 
end 

規格/ 功能 /controllers/statuses_spec.rb

require 'spec_helper' 

describe StatusesController do 
    describe "routing" do 
    # contains only capybara methods and so it passes 
    it "contains welcome message" do 
     visit("/statuses") 
     page.should have_content("All of our statuses ") 
    end 

    # contains rspec methods and so I recieve a no method failure 
    it "responds with 200" do 
     get("/statuses").should respond_with 200 
    end 

    end 
end 

輸出:

13:17:44 - INFO - Running: spec/features/controllers/statuses_spec.rb 
.F 

Failures: 

    1) StatusesController routing responds with 200 
    Failure/Error: get("/statuses").should respond_with 200 
    NoMethodError: 
     undefined method `respond_with' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x00000101787560> 
    # ./spec/features/controllers/statuses_spec.rb:12:in `block (3 levels) in <top (required)>' 

Finished in 0.40626 seconds 
2 examples, 1 failure 

我敢打賭,這是將這些規格放置在「功能」目錄中,但我需要將它們放置在「功能」目錄中以利用水豚方法。

因此,我需要將所有在'features'中使用capybara方法的規範以及所有在'spec'中使用rspec方法的規範放在一起?希望不是。我應該如何設置我的文件?

+0

你是控制器測試的混合方法和功能規格的方法 – apneadiving

+0

我幾個小時前纔開始使用rspec和水豚......你能解釋一下嗎? – Starkers

+0

要簡短:在'specs/features'文件夾中,你只能在路徑中使用'visit',填充表單並點擊etc ...在'specs/controller'中使用'get'或任何http動詞來確定你的控制器按照您的意願運行 – apneadiving

回答

0

explanation here

使用RSpec的要求規格與您的應用程序作爲一個 HTTP API測試相互作用。爲此,請使用如下方法:get,post,put,delete 並斷言與response

使用RSpec功能規格(帶水豚)來測試您的應用程序,因爲 用戶可能會與其交互。爲此,請使用visit和 等方法對page作出斷言。

+0

有人需要指出 - 非Capybara RSpec測試與實際的Rails應用程序的存根版本在同一個「VM」中運行。 Capybara測試運行在Ruby VM中,通過真實的HTTP驅動瀏覽器訪問網站。因此,從水豚測試到應用程序方法的任何調用都不會與測試目標位於同一個VM中,因此這樣的調用沒有用處,所以當水豚測試開始時,系統不會加載它們。 – Phlip

相關問題