Rspec:引擎路径助手

时间:2013-05-03 13:49:12

标签: ruby-on-rails rspec

路线:

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

constoller spec:

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
....

我有错误:

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等引擎路径助手?

2 个答案:

答案 0 :(得分:3)

我正在使用Rails 4和Rspec 3.我通过在规范中包含路由助手来实现它,如下所示:

describe Users::Test do

  include Users::Engine.routes.url_helpers

  it "sample" do
    get :test2
    response.should redirect_to step1_path
  end

end

答案 1 :(得分:2)

您的RSpec路由需要设置为引擎路由集,而不是引擎控制器测试的默认全局Rails.routes。

describe Users::Test do

  routes { Users::Engine.routes }

  it "sample" do
    get :test2
    response.should redirect_to step1_path
  end

end