2012-08-08 13 views
6

Sono completamente bloccato qui e spero che qualcuno possa indicarmi la giusta direzione. Sto cercando di usare rspec per testare i miei webroutes. Ho seguito l'esempio qui:NoMethodError: metodo non definito `get 'durante l'esecuzione di rspec e effettuare chiamate get/visit

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

con il mio file spec essere nominato: api_tests_spec.rb nelle specifiche/richieste cartella.

Il file è il seguente:

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 

Purtroppo il messaggio di errore sto ottenendo è la seguente:

1) APITests GET/regioni dovrebbe restituire una valida risposta Fallimento/Errore: ottenere " /regions.json" NoMethodError: metodo non definito` get' per # # ./api_tests_spec.rb:7

qualcuno ha alcuna visione per cui le metodo non può essere trovato? Tutto ciò che ho letto sembra suggerire che qualcosa non viene incluso correttamente, ma la soluzione sembra essere sempre quella di spostare il file nella cartella delle richieste (dove è già il mio). Grazie in anticipo!

Il file spec_helper.rb assomiglia a questo:

# 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

Come stai eseguendo il test? Cosa c'è nel tuo spec_helper? – Gazler

+0

bundle exec rspec api_tests_spec.rb, spec_helper aggiunto come modifica – akhalsa

risposta

6

Ok, dopo la prima risposta che ho postato sembrava funzionare per un po ', si è fermato (Ancora non è sicuro perché ?!)

Tuttavia , questa modifica al file api_tests_spec.rb lo fissa:

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 

qualcuno ha qualche idea del perché la: => tipo: è necessaria richiesta? Ho pensato che inserendo questo nella cartella delle richieste sotto di esso dovresti assumere che erano richieste?

+0

Sei sicuro che dovresti usare ': type =>: request', invece di': type =>: controller'? Penso che RSpec cerchi di dedurre il tipo dall'oggetto che stai testando. Quindi se l'oggetto è ad es. 'ApiController' e eredita da' ApplicationController', RSpec può dire che è un test del controller. –

+0

Alla fine di [questo problema] (https://github.com/rspec/rspec-rails/issues/392), si dice che l'inferenza avviene in base al nome della cartella e non al tipo di oggetto. Ho dimenticato di dire che con il tuo nome di "APITEST", l'inferenza è difficile per me senza vedere quella lezione. La tua specifica è correttamente nella directory 'controllers'? –

Problemi correlati