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

时间:2011-07-14 19:43:29

标签: 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 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)>'

当我从路由中删除scope语句时,测试工作正常。有没有办法“告知”RSpec的路线范围?

提前致谢!

1 个答案:

答案 0 :(得分:6)

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

get :new, :apikey => "something"

此外,您应该将下一行的期望更改为:

assigns[:shop].should_not be_nil

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