[英]Rspec: engine path helper
routes: 路线:
Users::Engine.routes.draw do
get "test/step1", to: "test#step1", as: "step1"
get "test/step2", to: "test#step2", as: "step2"
end
constoller spec: constoller规格:
describe Users::Test do
it "sample" do
get :test2, use_route: "users"
response.should redirect_to users.step1_path
end
end
rake routes: 耙路:
Routes for Users::Engine:
step1 GET /test/step1(.:format) users/test#step1
step2 GET /test/step2(.:format) users/test#step2
....
I have error: 我有错误:
Failure/Error: response.should redirect_to users.step1_path
NameError:
undefined local variable or method `users' for #<RSpec::Core::ExampleGroup::Nested_9::Nested_3:0xb7496f8>
How I can use engine path helpers like users.step1_path
in the controller specs? 如何在控制器规格中使用像users.step1_path
这样的引擎路径助手?
I'm using Rails 4 and Rspec 3. I got it working by including the route helpers in the spec, like this: 我使用的是Rails 4和Rspec3。我在规范中加入了路由助手,使之正常运行,如下所示:
describe Users::Test do
include Users::Engine.routes.url_helpers
it "sample" do
get :test2
response.should redirect_to step1_path
end
end
Your RSpec routes need to be set to your engines route set rather than the default global Rails.routes for your engines controller tests. 您需要将RSpec路由设置为引擎路由集,而不是引擎控制器测试的默认全局Rails.routes。
describe Users::Test do
routes { Users::Engine.routes }
it "sample" do
get :test2
response.should redirect_to step1_path
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.