Rspec集成测试:路由不可用

iGE*_*GEL 0 integration-testing rspec routes ruby-on-rails

我正在尝试编写集成测试测试,如果我的控制器为已删除的文章(仅标记为已删除)引发了ActiveRecord :: RecordNotFound执行:

it "should return 404 for deleted articles" do
  @article = Factory :article, :status => "deleted"
  expect { get edit_article_path(:id => @article.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
Run Code Online (Sandbox Code Playgroud)

这些测试在控制器规范中工作正常,但在规范/请求内部我收到此错误:

expected ActiveRecord::RecordNotFound, got #<ActionController::RoutingError: No route matches {:action=>"edit", :controller=>"articles", :id=>595}>
Run Code Online (Sandbox Code Playgroud)

所以它正确查找我的路线(因为它知道控制器等),但仍然会引发错误.这是为什么?

谢谢,约翰内斯

Emi*_*ily 5

如果此测试在您的spec/controllers目录中,那么get您正在调用的方法不期望路径,它期望您要调用的控制器操作的符号.因此,您需要将expect行更改为:

expect { get :edit, :id => @article.id }.to raise_error(ActiveRecord::RecordNotFound)
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看RSpec Controller Spec文档.