Rspec 集成测试:路由不可用

发布于 2024-11-30 10:32:22 字数 606 浏览 0 评论 0 原文

我试图编写一个集成测试,如果我的控制器对已删除的文章(仅标记为已删除)引发 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

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

expected ActiveRecord::RecordNotFound, got #<ActionController::RoutingError: No route matches {:action=>"edit", :controller=>"articles", :id=>595}>

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

谢谢,约翰内斯

I was trying to write an integration test testing, if my controller raises a ActiveRecord::RecordNotFound exeption for deleted articles (which are just marked as deleted):

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

These kind of tests work fine in a controller spec, but inside of the spec/requests I get this error:

expected ActiveRecord::RecordNotFound, got #<ActionController::RoutingError: No route matches {:action=>"edit", :controller=>"articles", :id=>595}>

So it looks up my routes correctly (since it knows the controller etc.) but still raises the error. Why is that?

Thanks, Johannes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-12-07 10:32:22

如果此测试位于您的 spec/controllers 目录中,那么您调用的 get 方法不需要路径,它需要您想要的控制器操作的符号打电话。因此,您需要将 expect 行更改为:

expect { get :edit, :id => @article.id }.to raise_error(ActiveRecord::RecordNotFound)

看看 RSpec 控制器规范文档 了解更多信息。

If this test is in your spec/controllers directory, then the get method you're calling isn't expecting a path, it's expecting a symbol for the controller action you want to call. So you'd need to change the expect line to something like:

expect { get :edit, :id => @article.id }.to raise_error(ActiveRecord::RecordNotFound)

Take a look at the RSpec Controller Spec documentation for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文