31
我在我的控制器中有这段代码,并且想用功能测试来测试这个代码行。如何在我的Rails应用程序中测试ActiveRecord :: RecordNotFound?
raise ActiveRecord::RecordNotFound if @post.nil?
其断言方法我应该使用? 我使用内建的rails 2.3.5测试框架。
我这个代码试了一下:
test "should return 404 if page doesn't exist." do
get :show, :url => ["nothing", "here"]
assert_response :missing
end
,但它不为我工作。得到这个测试输出:
test_should_return_404_if_page_doesn't_exist.(PageControllerTest):
ActiveRecord::RecordNotFound: ActiveRecord::RecordNotFound
app/controllers/page_controller.rb:7:in `show'
/test/functional/page_controller_test.rb:21:in `test_should_return_404_if_page_doesn't_exist.'
谢谢你,我喜欢那样! – xaver23 2010-03-22 14:19:26
我喜欢在我的ApplicationController(application_controller.rb)中使用'rescue_from ActiveRecord :: RecordNotFound',因为我不喜欢'assert_raises'块。 – 2011-01-02 22:40:48