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 回答