be_routable
匹配器
be_routable
匹配器最适合与 should_not
一起使用,以指定给定路由不应可路由。它在路由规范 (在 spec/routing 中) 和控制器规范 (在 spec/controllers 中) 中可用。
指定可路由路由不应可路由 (失败)
假设一个名为 “spec/routing/widgetsroutingspec.rb” 的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "does not route to widgets" do
expect(:get => "/widgets").not_to be_routable
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
则输出应包含 “1 个示例,1 个失败”。
指定不可路由路由不应可路由 (通过)
假设一个名为 “spec/routing/widgetsroutingspec.rb” 的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "does not route to widgets/foo/bar" do
expect(:get => "/widgets/foo/bar").not_to be_routable
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
则所有示例都应通过。
指定可路由路由应可路由 (通过)
假设一个名为 “spec/routing/widgetsroutingspec.rb” 的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes to /widgets" do
expect(:get => "/widgets").to be_routable
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
则所有示例都应通过。
指定不可路由路由应可路由 (失败)
假设一个名为 “spec/routing/widgetsroutingspec.rb” 的文件,其中包含
require "rails_helper"
RSpec.describe "routes for Widgets", type: :routing do
it "routes to widgets/foo/bar" do
expect(:get => "/widgets/foo/bar").to be_routable
end
end
当我运行 rspec spec/routing/widgets_routing_spec.rb
则输出应包含 “1 个示例,1 个失败”。
在控制器规范中使用 be_routable
假设一个名为 “spec/controllers/widgetscontrollerspec.rb” 的文件,其中包含
require "rails_helper"
RSpec.describe WidgetsController, type: :controller do
it "routes to /widgets" do
expect(:get => "/widgets").to be_routable
end
end
当我运行 rspec spec/controllers/widgets_controller_spec.rb
则所有示例都应通过。