我以前从未使用过单位测试但只使用过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.
这里可能有什么问题?感谢
答案 0 :(得分:0)
测试的@controller应该来自哪里?如果它是由ActionController :: TestCase创建的,那么您可能想在自己的设置函数中调用超类的设置?