Ruby on rails 3 作用域路由上RSpec控制器测试的路由错误

Ruby on rails 3 作用域路由上RSpec控制器测试的路由错误,ruby-on-rails-3,controller,rspec,scope,routes,Ruby On Rails 3,Controller,Rspec,Scope,Routes,我有一条路线,看起来像这样: scope "4" do scope "public" do scope ":apikey" do resources :shops end end end describe ShopsController do describe "when responding to a GET" do context "#new" do it "should create a new instance of th

我有一条路线,看起来像这样:

scope "4" do
  scope "public" do
    scope ":apikey" do
      resources :shops
    end
  end
end
describe ShopsController do

  describe "when responding to a GET" do

    context "#new" do
      it "should create a new instance of the shop class" do
        get :new
        @shop.should_not_be_nil
      end
    end

  end

end
还有一系列控制器规格,其示例如下:

scope "4" do
  scope "public" do
    scope ":apikey" do
      resources :shops
    end
  end
end
describe ShopsController do

  describe "when responding to a GET" do

    context "#new" do
      it "should create a new instance of the shop class" do
        get :new
        @shop.should_not_be_nil
      end
    end

  end

end
在rake路由中,以及通过web浏览器,此控制器/操作工作正常。但是,RSpec抛出:

1) ShopsController when responding to a GET#new should create a new instance of the shop class
 Failure/Error: get :new
 ActionController::RoutingError:
   No route matches {:controller=>"shops", :action=>"new"}
 # ./spec/controllers/shops_controller_spec.rb:9:in `block (4 levels) in <top (required)>'
1)ShopsController在响应GET#new时应创建shop类的新实例
失败/错误:获取:新建
ActionController::路由错误:
没有路由匹配{:controller=>“shops”,:action=>“new”}
#./spec/controllers/shops\u controller\u spec.rb:9:in'block(4层)in'
当我从路由中删除
scope
语句时,测试工作正常。有没有办法“通知”RSpec路线范围


提前谢谢

根据您的路线,
:apikey
是必需的参数,因此您需要将其添加到get中:

get :new, :apikey => "something"
此外,您还应将下一行中的期望更改为:

assigns[:shop].should_not be_nil
使用
赋值
检查控制器的实例变量,并用空格而不是下划线将
不应
与匹配器分开。最后一点需要一些时间来适应