2012-08-08 13 views
6

मैं पूरी तरह से यहां फंस गया हूं, और उम्मीद करता हूं कि कोई मुझे सही दिशा में इंगित कर सकता है। मैं अपने webroutes का परीक्षण करने के लिए rspec का उपयोग करने की कोशिश कर रहा हूँ। मेरी कल्पना फ़ाइल नामित होने के साथNoMethodError: rspec चलाते समय अपरिभाषित विधि 'प्राप्त करें' और कॉल प्राप्त करें/विज़िट करें

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

: मैं यहाँ उदाहरण का पालन कल्पना में api_tests_spec.rb/फ़ोल्डर अनुरोध करता है।

इस प्रकार
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 प्राप्त/क्षेत्रों एक वैध प्रतिक्रिया विफलता/त्रुटि लौटानी चाहिए:

फ़ाइल प्रकार है मिल " /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

+0

बंडल exec rspec api_tests_spec.rb, spec_helper एक संपादन के रूप में जोड़ा गया – akhalsa

उत्तर

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 

किसी को भी किसी भी विचार क्यों है: type =>: अनुरोध की आवश्यकता है? मैंने सोचा कि इसे नीचे दिए गए अनुरोध फ़ोल्डर में रखकर यह मानना ​​चाहिए कि वे अनुरोध थे?

+0

क्या आप सुनिश्चित हैं कि आपको ': type =>: request'' के बजाय ': type =>: request' का उपयोग करना चाहिए? मुझे लगता है कि आरएसपीईसी आपके द्वारा परीक्षण की जा रही वस्तु से प्रकार का अनुमान लगाने की कोशिश करता है। तो अगर वस्तु उदा। 'एपीकंट्रोलर' और इसे 'एप्लिकेशन कंट्रोलर' से प्राप्त होता है, आरएसपीईसी यह एक नियंत्रक परीक्षण बता सकता है। –

+0

[इस मुद्दे] के नीचे [https://github.com/rspec/rspec-rails/issues/392), यह कहता है कि अनुमान फ़ोल्डर नाम के आधार पर होता है और ऑब्जेक्ट प्रकार नहीं। मैं यह कहना भूल गया था कि 'एपीआईटीटीएस' के नाम से, उस कक्षा को देखे बिना मेरे लिए अनुमान मुश्किल है। क्या आपका spec 'नियंत्रकों' निर्देशिका में ठीक से है? –

संबंधित मुद्दे