2

私はこれまでユニットテストを使用したことがありませんが、Rspecのみを使用しました。だから多分ここにいくつかのばかげた間違いがあります。

私は持っていますCountriesController

class CountriesController < ApplicationController

  def create
    @country = Country.new(params[:country])

    respond_to do |format|
      if @country.save
        format.html { redirect_to(@country, :notice => 'Country was successfully created.') }
        format.xml  { render :xml => @country, :status => :created, :location => @country }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @country.errors, :status => :unprocessable_entity }
      end
    end
  end
end

そしてそれをテストcountries_controller_test.rbします:

class CountriesControllerTest < ActionController::TestCase
  should_not_respond_to_actions :new => :get, :destroy => :get

  setup do
    @country = countries(:one)
  end

  test "should create country" do
    assert_difference('Country.count') do
      post :create, :country => @country.attributes.merge({ :code => Time.now.to_s })
    end

    assert_redirected_to country_path(assigns(:country))
  end
  ...
end

名前の規則を知っている限り、すべてが問題ないように見えますが、エラーが発生しました。

1) Error:
test_should_create_country(CountriesControllerTest):
RuntimeError: @controller is nil: make sure you set it in your test's setup method.

ここで何が問題になる可能性がありますか?ありがとう

4

1 に答える 1

0

テストの@controllerはどこから来ると思われますか?ActionController :: TestCaseによって作成された場合は、独自のセットアップ関数でスーパークラスのセットアップを呼び出したいと思うかもしれません。

于 2013-02-12T13:03:21.383 に答える