在Scoped路径上使用RSpec控制器测试的RoutingError

ian*_*anm 3 scope controller rspec routes ruby-on-rails-3

所以我有一条看起来像这样的路线:

scope "4" do
  scope "public" do
    scope ":apikey" do
      resources :shops
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

还有一堆控制器规格,其中一个例子如下:

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
Run Code Online (Sandbox Code Playgroud)

在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)>'
Run Code Online (Sandbox Code Playgroud)

当我scope从路线中删除语句时,测试工作正常.有没有办法"通知"RSpec的路线范围?

提前致谢!

Rob*_*vis 6

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

get :new, :apikey => "something"
Run Code Online (Sandbox Code Playgroud)

您还应该在下一行中将期望值更改为:

assigns[:shop].should_not be_nil
Run Code Online (Sandbox Code Playgroud)

使用assigns检查控制器的实例变量,并分开should_not从匹配用空格,没有下划线.最后一点需要一些时间来适应.