2012-08-08 16 views
6

我完全陷在這裏,希望有人能指出我正確的方向。我正在嘗試使用rspec來測試我的網絡路由。我跟着這裏的例子:NoMethodError:在運行rspec和獲取/訪問電話時獲取'unde'方法

https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

與我的規格文件被命名:api_tests_spec.rb在spec /請求文件夾。

文件如下:

require 'spec_helper' 

describe "APITests" do 
    describe "GET /regions" do 
    it "should return a valid response" do 
     # Run the generator again with the --webrat flag if you want to use webrat methods/matchers 
     get "/regions.json" 
     response.status.should be(200) 
     #print response.body 
    end 
    end 
end 

不幸的是我收到故障信息如下:

1)APITests GET /地區應返回一個有效的響應 故障/錯誤:得到「 /regions.json」 NoMethodError: 未定義的方法'得到」爲# #./api_tests_spec.rb:7

沒有人有任何深入瞭解爲何方法找不到?我讀過的所有東西似乎都暗示某些東西沒有被正確包含,但解決方案似乎總是將文件移動到請求文件夾中(我的位置已經存在)。提前致謝!

的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' 

# 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} 

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 

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

    # 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 
end 
+0

你如何運行測試?你的spec_helper中有什麼? – Gazler 2012-08-08 18:27:28

+0

bundle exec rspec api_tests_spec.rb,spec_helper作爲編輯添加 – akhalsa 2012-08-08 18:32:46

回答

6

好吧,我張貼的第一個答案似乎對於一些工作後,就停止了(不過不知道爲什麼?!)

然而,使這一變化在api_tests_spec.rb文件修復它:

require 'spec_helper' 

describe "APITests", :type => :request do 
    describe "GET /regions" do 
    it "should return a valid response" do 
     # Run the generator again with the --webrat flag if you want to use webrat methods/matchers 
     get "/regions.json" 
     response.status.should be(200) 
     #print response.body 
    end 
    end 
end 

有沒有人有,爲什麼任何想法:類型=>:是必需的要求嗎?我認爲只要把它放在請求文件夾下它應該假設他們是請求?

+0

您確定應該使用':type =>:request',而不是':type =>:controller'嗎?我認爲RSpec試圖從你正在測試的對象中推斷出類型。所以如果物體是例如'ApiController'並且它從'ApplicationController'繼承,RSpec可以告訴它是一個控制器測試。 – 2014-06-10 01:03:26

+0

在[本期]的底部(https://github.com/rspec/rspec-rails/issues/392),它表示推斷基於文件夾名稱而不是對象類型。我忘了說你的名字是'APITests',這個推論對我來說很難,因爲我沒有看到那個課。你的規格是否在'controllers'目錄中正確? – 2014-06-10 01:05:50