繁体   English   中英

Rspec:引擎路径帮助器

[英]Rspec: engine path helper

路线:

Users::Engine.routes.draw do
  get "test/step1", to: "test#step1", as: "step1"
  get "test/step2", to: "test#step2", as: "step2"
end

constoller规格:

describe Users::Test do
  it "sample" do
    get :test2, use_route: "users"
    response.should redirect_to users.step1_path
  end
end

耙路:

Routes for Users::Engine:
  step1 GET    /test/step1(.:format)                    users/test#step1
  step2 GET    /test/step2(.:format)                    users/test#step2
....

我有错误:

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>

如何在控制器规格中使用像users.step1_path这样的引擎路径助手?

我使用的是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

您需要将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM