私はこれまでユニットテストを使用したことがありませんが、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.
ここで何が問題になる可能性がありますか?ありがとう