首次发布这个有用的社区,但自编程之旅开始以来一直在使用它。我是rails的新手并且在编写测试时遇到了麻烦。到目前为止,我一直在尝试编写基于Authlogic perishable token guide的控制器测试。
但是在测试过程中,它一直停留在线上
#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)>'
在阅读one of the posted answers后,似乎我没有正确创建存根/模拟,这就是为什么它不断获取redirect_to(302)而不是继续测试。 (find_using_pt是来自Authlogic的方法)。因此,问题在于redirect_to行,因为它永远不会继续使用控制器/页面呈现
任何建议都将不胜感激。感谢。