2012-08-08 12 views
6

Burada tamamen takılıyorum ve birisinin beni doğru yönde göstermesini bekliyorum. Web rotalarımı test etmek için rspec kullanmaya çalışıyorum. Burada örnek izledi:NoMethodError: rspec çalışırken ve get/ziyaret çağrıları yaparken undefined method `get '

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

benim özellik dosyası adı olan: api_tests_spec.rb spesifikasyonundaki/klasör ister.

aşağıdaki
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 

Ne yazık ki alıyorum başarısızlık mesajı olarak:

1) APITests GET/bölgeler geçerli bir yanıt Başarısızlık/Hata dönmelidir şu şekildedir:

dosyasıdır olsun " /regions.json" NoMethodError: tanımsız yöntem` # # için 'get ./api_tests_spec.rb:7

kimse neden herhangi bir fikir var mı yöntem bulunamadı Okuduğum her şey bir şeyin düzgün bir şekilde yer almamasını öneriyor gibi görünüyor, ancak çözüm her zaman dosyayı istek klasörüne (benimki zaten olduğu gibi) taşımak gibi görünüyor. Şimdiden teşekkürler!

spec_helper.rb dosyası şuna benzer: Ben yayınlanmıştır ilk cevabı biraz çalışmak gibiydi sonra

# 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

Testi nasıl çalıştırıyorsunuz? Spec_helper'ınızda ne var? – Gazler

+0

bundle exec rspec api_tests_spec.rb, spec_helper düzenleme olarak eklendi – akhalsa

cevap

6

Tamam, bu durdurdu (hala emin değil neden ?!) Ancak

, api_tests_spec.rb dosyasına bu değişikliği yapma bunu düzeltir:

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 

kimse neden bir fikrin var mı: type =>: istek gereklidir? Sadece istekleri klasörüne yerleştirerek istekleri olduğunu varsaymayı düşündüm?

+0

': type =>: request' yerine': type =>: controller' yerine kullanmanız gerektiğinden emin misiniz? Sanırım RSpec, test ettiğiniz nesneyi türden çıkarmaya çalışır. Yani eğer nesne ör. 'ApiController' ve 'ApplicationController''dan devralır, RSpec bir denetleyici testi olduğunu söyleyebilir. –

+0

[Bu sayının] alt kısmında (https://github.com/rspec/rspec-rails/issues/392), çıkarımın nesne türüne değil, nesne adına göre gerçekleştiğini söylüyor. Ben 'APITests' ismiyle, bu sınıfı görmeden çıkarımın benim için zor olduğunu söylemeyi unuttum. Spesifikasyonunuz 'controllers' dizininde doğru mu? –

İlgili konular