1

にクラス メソッドを追加するプラグインをActionController::Base作成しているので、機能テストでは、テスト対象の単純なコントローラーを作成しています。以下の例は、基本的に何もしないように完全に削除されています。テストは まで成功しますがassert_template、失敗します。

require 'rubygems'
require 'test/unit'
require 'active_support'
require 'active_support/test_case'
require 'action_controller'
require 'action_controller/test_process'

class UnderTestController < ActionController::Base
  def index; end
end
ActionController::Routing::Routes.draw {|map| map.resources :under_test }

class MyPluginTest < ActionController::TestCase
  def setup
    @controller = UnderTestController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  test "should render the correct template" do
    get :index
    assert_template :index  # everything works except this assert
  end
end

上記のファイルを実行した結果は次のとおりです。

Loaded suite /Users/jjulian/sandbox/vendor/plugins/my_plugin/test/my_plugin_test
Started
E 
Finished in 0.014567 seconds.

  1) Error:
test_should_render_the_correct_template(MyPluginTest):
NoMethodError: undefined method `[]' for nil:NilClass
method assert_template in response_assertions.rb at line 103
method clean_backtrace in test_case.rb at line 114
method assert_template in response_assertions.rb at line 100
method test_should_render_the_correct_template in so_test.rb at line 22
method __send__ in setup_and_teardown.rb at line 62
method run in setup_and_teardown.rb at line 62

1 tests, 0 assertions, 0 failures, 1 errors

私の質問:assert_template作業を許可するために欠けているものは何ですか? 正しいライブラリが必要ですか? 正しい TestCase クラスを拡張していますか?

4

2 に答える 2

3

私はそれを考え出した。まず、テンプレートが欠落しているのは、view_pathテスト対象のコントローラーで明示的に設定する必要があるためです。

before_filter {|c| c.prepend_view_path(File.join(File.dirname(__FILE__), 'fixtures')) }

にダミーのerbファイルを作成しましたtest/fixtures/under_test/index.erb。これで、のnilエラーに戻りますresponse_assertions.rb。別のテストクラスを含める必要があるようです。

require 'action_view/test_case'

今では動作します。

1つの追加の注意:成功を決定するためにassert_template使用matchするため、この場合は、と同様にassert_template 'ind'成功しassert_template 'x'ます。

于 2009-11-27T17:27:35.280 に答える
1

テストの setup メソッドを だけに置き換えてみてくださいtests UnderTestController。Rails は、テスト コントローラー、リクエスト、およびレスポンスの設定を自動的に処理します。

于 2009-11-23T18:51:25.940 に答える