2011-04-28 5 views
8

मैं प्लगइन "foobar" के लिए परीक्षण विकसित करने की कोशिश कर रहा हूं जो कुछ मानक रेल सहायकों को संशोधित करता है।रेल 3 प्लगइन के लिए परीक्षण मार्ग उत्पन्न करने में त्रुटि?

# create the test model 
class Thing < ActiveRecord::Base 
end 

# create the test controller, which renders the included index template 
class ThingsController < ActionController::Base 
    def index 
    @things = Thing.all 
    format.html { render(:file => 'index') } 
    end 

    def destroy 
    @thing = Thing.find(params[:id]) 
    @thing.destroy 
    format.html { render(:file => 'index') } 
    end 
end 

# confirm that the test environment is working correctly 
class ThingsTest < ActiveSupport::TestCase 
    test "model is loaded correctly" do 
    assert_kind_of Thing, Thing.new 
    end 
end 

# confirm that the controller and routes are working correctly 
class ThingsControllerTest < ActionController::TestCase 
    test "should load index" do 
    with_routing do |set| 
     set.draw do 
     resources :things, :only => [:index, :destroy] 
     end 

     get :index 
     assert_response :success 
    end 
    end 
end 

जब मैं रेक चलाने के लिए, मैं निम्नलिखित उत्पादन:

test_should_load_index(ThingsControllerTest): 
NameError: undefined local variable or method `_routes' for ThingsController:Class 

किसी भी विचार है कि मैं क्या कर रहा हूँ विक्रेता/plugins/foobar/परीक्षण/foobar_test.rb में, मैं निम्नलिखित है गलत है जो मुझे मार्गों तक पहुंचने से रोक रहा है? मैं इस दिन रहा हूं और कोई फायदा नहीं हुआ, बहुत अधिक दस्तावेज खराब कर दिया। सहायता के लिए धन्यवाद!

+1

आपने किसी को समस्या को पुन: उत्पन्न करने में सक्षम होने के लिए पर्याप्त जानकारी प्रदान नहीं की है। रेल का क्या संस्करण? क्या रेक कार्य (डिफ़ॉल्ट कार्य विक्रेता में परीक्षण नहीं चलाता है)। आप अपना डेटाबेस स्कीमा कहां सेट अप करते हैं। जब मैं आपके द्वारा प्रदान किए गए परीक्षण को चलाने के लिए पर्याप्त अतिरिक्त संदर्भ प्रदान करता हूं, तो मुझे वह त्रुटि नहीं मिलती है। – John

+0

ईमानदार होने के लिए आप शायद इन दिनों एक मणि के साथ बेहतर होगा। – thomasfedb

उत्तर

1

मुझे रूबी 1.9.2 के साथ रेल 3.1.0.rc1 पर काम करने के लिए चीजें मिलती हैं, "The Basics of Creating Rails Plugins" रेल गाइड और असंगतता के कारण जब भी यह विस्फोट हो जाता है तो कोड को सही करता है।

# vendor/plugins/foobar/init.rb 
require 'foobar' 

# vendor/plugins/foobar/lib/foobar.rb 

%w{ models controllers helpers }.each do |dir| 
    path = File.join(File.dirname(__FILE__), 'app', dir) 
    $LOAD_PATH << path 
    ActiveSupport::Dependencies.autoload_paths << path 
    ActiveSupport::Dependencies.autoload_once_paths.delete(path) 
end 

# I'm not entirely sure this is the best way to add our plugin's views 
# to the view search path, but it works 
ActionController::Base.view_paths = 
    ActionController::Base.view_paths + 
    [ File.join(File.dirname(__FILE__), 'app', 'views') ] 

<!-- vendor/plugins/foobar/lib/app/views/things/index.html.erb --> 
<h1>Listing things</h1> 

<table> 
    <tr> 
    <th>Name</th> 
    <th></th> 
    </tr> 

<% @things.each do |thing| %> 
    <tr> 
    <td><%= thing.name %></td> 
    <td><%= link_to 'Destroy', thing, confirm: 'Are you sure?', method: :delete %></td> 
    </tr> 
<% end %> 
</table> 
:

Code$ rails new tester 
Code$ cd tester 
tester$ rails generate plugin foobar --with-generator 

तब डिफ़ॉल्ट लोगों के अलावा इन फ़ाइलों को प्राप्त करने के लिए तैयार किए गए कोड को संशोधित:

मैं चल रहा द्वारा शुरू किया


# vendor/plugins/foobar/test/database.yml 

sqlite: 
    :adapter: sqlite 
    :dbfile: vendor/plugins/foobar/test/foobar_plugin.sqlite.db 

sqlite3: 
    :adapter: sqlite3 
    :database: vendor/plugins/foobar/test/foobar_plugin.sqlite3.db 

postgresql: 
    :adapter: postgresql 
    :username: postgres 
    :password: postgres 
    :database: foobar_plugin_test 
    :min_messages: ERROR 

mysql: 
    :adapter: mysql 
    :host: localhost 
    :username: root 
    :password: password 
    :database: foobar_plugin_test 

# vendor/plugins/foobar/test/schema.rb 

ActiveRecord::Schema.define(:version => 0) do 
    create_table :things, :force => true do |t| 
    t.string :name 
    t.datetime :created_at 
    t.datetime :updated_at 
    end 
end 

# vendor/plugins/foobar/test/test_helper.rb 

ENV['RAILS_ENV'] = 'test' 
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' 

require 'test/unit' 
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) 

def load_schema 
    config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) 
    ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") 

    db_adapter = ENV['DB'] 

    # no db passed, try one of these fine config-free DBs before bombing. 
    db_adapter ||= 
    begin 
     require 'rubygems' 
     require 'sqlite' 
     'sqlite' 
    rescue MissingSourceFile 
     begin 
     require 'sqlite3' 
     'sqlite3' 
     rescue MissingSourceFile 
     end 
    end 

    if db_adapter.nil? 
    raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3." 
    end 

    ActiveRecord::Base.establish_connection(config[db_adapter]) 
    load(File.dirname(__FILE__) + "/schema.rb") 
    require File.dirname(__FILE__) + '/../init' 
end 

load_schema 

# vendor/plugins/foobar/test/foobar_test.rb 
require File.dirname(__FILE__) + '/test_helper' 

# create the test model 
class Thing < ActiveRecord::Base 
end 

# create the test controller, which renders the included index template 
class ThingsController < ActionController::Base 
    def index 
    @things = Thing.all 
    respond_to do |format| 
     format.html # index.html.erb 
    end 
    end 

    def destroy 
    @thing = Thing.find(params[:id]) 
    @thing.destroy 
    respond_to do |format| 
     format.html { redirect_to things_url } 
    end 
    end 
end 

# confirm that the test environment is working correctly 
class ThingsTest < ActiveSupport::TestCase 
    test "schema has loaded correctly" do 
    assert_equal [], Thing.all 
    end 

    test "model is loaded correctly" do 
    assert_kind_of Thing, Thing.new 
    end 
end 

# confirm that the controller and routes are working correctly 
class ThingsControllerTest < ActionController::TestCase 
    test "should load index" do 
    with_routing do |set| 
     set.draw do 
     resources :things, :only => [:index, :destroy] 
     end 

     get :index 
     assert_response :success 
    end 
    end 
end 

और अंत में, हमारे परीक्षण में सफ़ल:

tester$ cd vendor/plugins/foobar/ 
foobar$ rake 
-- create_table(:things, {:force=>true}) 
    -> 0.0059s 
-- initialize_schema_migrations_table() 
    -> 0.0002s 
-- assume_migrated_upto_version(0, ["db/migrate"]) 
    -> 0.0003s 
Loaded suite /Users/nick/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/rake_test_loader 
Started 
... 
Finished in 0.091642 seconds. 

3 tests, 3 assertions, 0 failures, 0 errors, 0 skips 
+0

'आरवीएम/रत्न/रूबी-1.9.2-पी 0/रत्न/रेक-0.9.2/lib/रेक/rake_test_loader' मुझे लगता है कि आपके पास रुबी 1.9.2 के साथ आरवीएम है, उम्मीद है कि रेल 3। मैं सोच रहा हूं क्योंकि यह नाम त्रुटि तब होती है जब आप एक अनिश्चित स्थिरता का उपयोग करते हैं। लेकिन मैं आपकी फाइलों में यहां ** ** रूट ** निरंतर नहीं देख सकता हूं। फ़ाइल _accidentally_ को संपादित करते समय मुझे आमतौर पर यह त्रुटि मिलती है। क्या आप गिट का उपयोग करते हैं? आप गिट diff कोशिश कर सकते हैं और एक कार्य मंच तक वापस भी कर सकते हैं। – YogiZoli

1

इस डीबगिंग के लिए आपका सबसे अच्छा शर्त

rake <task> --trace 

यह आपको एक बेहतर विचार दे देंगे करने के लिए नहीं है (यानी एक लाइन नंबर) जो _routes त्रुटि उत्पन्न कर रहा है।

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