Rails路由测试

时间:2015-09-04 12:47:41

标签: ruby-on-rails testing tdd

我是测试的新手,我有一个问题。

此代码测试该路由是否正确,好吗?

test "root should route to home#index" do
assert_routing "/", {controller: "home", action: "index"}
end

我如何测试相反的情况?我有

resources :sessions, :only=>[:new, :create, :destroy]

在我的路线中。我如何测试您无法进入会议#edit?

难道我不应该测试吗?

编辑:我忘了提及它,我使用的是默认工具。

2 个答案:

答案 0 :(得分:0)

如果您正在使用RSpec,则可以

it "does not route to sessions/edit" do
  expect(:get => "/sessions/edit").not_to be_routable
end

有关编写路由规范的详细信息,请参阅https://relishapp.com/rspec/rspec-rails/docs/routing-specs/be-routable-matcher

答案 1 :(得分:0)

你可以试试这个,它会失败,除非测试assert_generates引发“UrlGenerationError”=>无法生成会话的URL#edit

test "route should not exist" do
  begin
    assert_generates "sessions/edit", controller: :sessions, action: :edit
    flunk "Route to should not exist"
  rescue ActionController::UrlGenerationError
  end
end