1

この有益なコミュニティへの最初の投稿ですが、プログラミングの旅の開始以来、このコミュニティを使用しています。Rails は初めてで、テストを書くのに苦労しています。これまでのところ、 Authlogic の腐敗しやすいトークン ガイドに基づいてコントローラー テストを作成しようと試みてきました。

しかし、テスト中に、それはラインで立ち往生し続けます

#controller
@user = User.find_using_perishable_token(params[:activation_code], 1.week) || (redirect_to new_user_session_url and return)

そして、問題が発生するテストの行は次のとおりです。

#test
User.any_instance.stub(:find_using_perishable_token) {mock_model(User, stubs(:active => true)) }
get :create, :activation_code => 'valid'
response.should render_template 'new'

response.code.should == "200"
#the test response is 302, not 200
response.should redirect_to("/")
#the test shows the path as user_sessions/new,
#meaning it didn't pass the condition in the controller with the find_using_pt method

テスト出力は次のとおりです。

Failure/Error: response.should render_template 'new' expecting <"new"> but rendering with <""> 
# ./spec/controllers/activations_controller_spec.rb:18:in `block (3 levels) in <top (required)>'

投稿された回答の1つを読んだ後、スタブ/モックを正しく作成していないようです。そのため、テストを続行するのではなく、redirect_to (302) を取得し続けます。(find_using_pt は Authlogic のメソッドです)。したがって、コントローラー/ページのレンダリングを続行しないため、redirect_to 行に問題があります。

アドバイスをいただければ幸いです。ありがとう。

4

0 に答える 0