我以前从未使用过单元测试,但只使用过 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.
这里可能有什么问题?谢谢