如何使用RSpec测试Rails 4应用程序并从Railscast#53(修订版)中自定义异常处理?

时间:2014-03-21 02:04:53

标签: ruby-on-rails redirect rspec railscasts custom-exceptions

使用this Railscast episodeActiveRecord::RecordNotFound错误最终会重定向到ErrorsController和/app/views/errors/404.html.erb视图。但是,我在我的一个测试中有以下内容(Item belongs_to List):

scenario "item cannot be created without a list" do
  visit new_item_path
  # Because it's not the proper nested new_list_item_path(@some_list) path,
  # @list is not created in the ItemsController.
  page.should have_content('Not Found')
  page.status_code.should be 404
end

但是,测试失败并带有以下内容:

Failure/Error: visit new_item_path
ActiveRecord::RecordNotFound:
  Couldn't find List without an ID

因此控制器在需要列表时正常工作,但测试失败,因为它没有考虑应该将用户发送到404的自定义异常处理。

如何正确测试重定向?

0 个答案:

没有答案