ruby – 为Rails 3插件生成测试路由时出错?

前端之家收集整理的这篇文章主要介绍了ruby – 为Rails 3插件生成测试路由时出错?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试为插件“foobar”开发测试,修改一些标准的Rails助手.在vendor / plugins / foobar / test / foobar_test.rb中,我有以下内容
  1. # create the test model
  2. class Thing < ActiveRecord::Base
  3. end
  4.  
  5. # create the test controller,which renders the included index template
  6. class ThingsController < ActionController::Base
  7. def index
  8. @things = Thing.all
  9. format.html { render(:file => 'index') }
  10. end
  11.  
  12. def destroy
  13. @thing = Thing.find(params[:id])
  14. @thing.destroy
  15. format.html { render(:file => 'index') }
  16. end
  17. end
  18.  
  19. # confirm that the test environment is working correctly
  20. class ThingsTest < ActiveSupport::TestCase
  21. test "model is loaded correctly" do
  22. assert_kind_of Thing,Thing.new
  23. end
  24. end
  25.  
  26. # confirm that the controller and routes are working correctly
  27. class ThingsControllerTest < ActionController::TestCase
  28. test "should load index" do
  29. with_routing do |set|
  30. set.draw do
  31. resources :things,:only => [:index,:destroy]
  32. end
  33.  
  34. get :index
  35. assert_response :success
  36. end
  37. end
  38. end

当我运行rake时,我得到以下输出

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

知道我做错了什么阻止我访问路线?我已经在这里待了好几天并且搜索了很多文档,但都无济于事.谢谢您的帮助!

解决方法

我使用Ruby 1.9.2在Rails 3.1.0.rc1上工作,遵循 “The Basics of Creating Rails Plugins” Rails指南并在因不兼容而爆炸时更正代码.

我开始跑步:

  1. Code$rails new tester
  2. Code$cd tester
  3. tester$rails generate plugin foobar --with-generator

然后修改生成代码获取除默认代码之外的这些文件

  1. # vendor/plugins/foobar/init.rb
  2. require 'foobar'
  1. # vendor/plugins/foobar/lib/foobar.rb
  2.  
  3. %w{ models controllers helpers }.each do |dir|
  4. path = File.join(File.dirname(__FILE__),'app',dir)
  5. $LOAD_PATH << path
  6. ActiveSupport::Dependencies.autoload_paths << path
  7. ActiveSupport::Dependencies.autoload_once_paths.delete(path)
  8. end
  9.  
  10. # I'm not entirely sure this is the best way to add our plugin's views
  11. # to the view search path,but it works
  12. ActionController::Base.view_paths =
  13. ActionController::Base.view_paths +
  14. [ File.join(File.dirname(__FILE__),'views') ]
  1. <!-- vendor/plugins/foobar/lib/app/views/things/index.html.erb -->
  2. <h1>Listing things</h1>
  3.  
  4. <table>
  5. <tr>
  6. <th>Name</th>
  7. <th></th>
  8. </tr>
  9.  
  10. <% @things.each do |thing| %>
  11. <tr>
  12. <td><%= thing.name %></td>
  13. <td><%= link_to 'Destroy',thing,confirm: 'Are you sure?',method: :delete %></td>
  14. </tr>
  15. <% end %>
  16. </table>
  1. # vendor/plugins/foobar/test/database.yml
  2.  
  3. sqlite:
  4. :adapter: sqlite
  5. :dbfile: vendor/plugins/foobar/test/foobar_plugin.sqlite.db
  6.  
  7. sqlite3:
  8. :adapter: sqlite3
  9. :database: vendor/plugins/foobar/test/foobar_plugin.sqlite3.db
  10.  
  11. postgresql:
  12. :adapter: postgresql
  13. :username: postgres
  14. :password: postgres
  15. :database: foobar_plugin_test
  16. :min_messages: ERROR
  17.  
  18. MysqL:
  19. :adapter: MysqL
  20. :host: localhost
  21. :username: root
  22. :password: password
  23. :database: foobar_plugin_test
  1. # vendor/plugins/foobar/test/schema.rb
  2.  
  3. ActiveRecord::Schema.define(:version => 0) do
  4. create_table :things,:force => true do |t|
  5. t.string :name
  6. t.datetime :created_at
  7. t.datetime :updated_at
  8. end
  9. end
  1. # vendor/plugins/foobar/test/test_helper.rb
  2.  
  3. ENV['RAILS_ENV'] = 'test'
  4. ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
  5.  
  6. require 'test/unit'
  7. require File.expand_path(File.join(ENV['RAILS_ROOT'],'config/environment.rb'))
  8.  
  9. def load_schema
  10. config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
  11. ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
  12.  
  13. db_adapter = ENV['DB']
  14.  
  15. # no db passed,try one of these fine config-free DBs before bombing.
  16. db_adapter ||=
  17. begin
  18. require 'rubygems'
  19. require 'sqlite'
  20. 'sqlite'
  21. rescue MissingSourceFile
  22. begin
  23. require 'sqlite3'
  24. 'sqlite3'
  25. rescue MissingSourceFile
  26. end
  27. end
  28.  
  29. if db_adapter.nil?
  30. raise "No DB Adapter selected. Pass the DB= option to pick one,or install sqlite or sqlite3."
  31. end
  32.  
  33. ActiveRecord::Base.establish_connection(config[db_adapter])
  34. load(File.dirname(__FILE__) + "/schema.rb")
  35. require File.dirname(__FILE__) + '/../init'
  36. end
  37.  
  38. load_schema
  1. # vendor/plugins/foobar/test/foobar_test.rb
  2. require File.dirname(__FILE__) + '/test_helper'
  3.  
  4. # create the test model
  5. class Thing < ActiveRecord::Base
  6. end
  7.  
  8. # create the test controller,which renders the included index template
  9. class ThingsController < ActionController::Base
  10. def index
  11. @things = Thing.all
  12. respond_to do |format|
  13. format.html # index.html.erb
  14. end
  15. end
  16.  
  17. def destroy
  18. @thing = Thing.find(params[:id])
  19. @thing.destroy
  20. respond_to do |format|
  21. format.html { redirect_to things_url }
  22. end
  23. end
  24. end
  25.  
  26. # confirm that the test environment is working correctly
  27. class ThingsTest < ActiveSupport::TestCase
  28. test "schema has loaded correctly" do
  29. assert_equal [],Thing.all
  30. end
  31.  
  32. test "model is loaded correctly" do
  33. assert_kind_of Thing,:destroy]
  34. end
  35.  
  36. get :index
  37. assert_response :success
  38. end
  39. end
  40. end

最后,我们的测试通过:

  1. tester$cd vendor/plugins/foobar/
  2. foobar$rake
  3. -- create_table(:things,{:force=>true})
  4. -> 0.0059s
  5. -- initialize_schema_migrations_table()
  6. -> 0.0002s
  7. -- assume_migrated_upto_version(0,["db/migrate"])
  8. -> 0.0003s
  9. Loaded suite /Users/nick/.rvm/gems/ruby-1.9.2-p0/gems/rake-0.9.2/lib/rake/rake_test_loader
  10. Started
  11. ...
  12. Finished in 0.091642 seconds.
  13.  
  14. 3 tests,3 assertions,0 failures,0 errors,0 skips

猜你在找的Ruby相关文章