使用 RSpec 控制器在范围路由上进行测试时出现 RoutingError

发布于 2024-11-19 21:10:27 字数 910 浏览 0 评论 0原文

所以我有一条如下所示的路线:

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 路线中,以及通过网络浏览器,此控制器/操作工作正常。但是,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 路由范围?

提前致谢!

So I have a route that looks like this:

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

And a bunch of controller specs, an example of which looks like this:

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

In rake routes, as well as via a web browser, this controller/action works fine. However, RSpec throws:

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)>'

When I remove the scope statements from the route, the tests work fine. Is there a way to "inform" RSpec of the route scopes?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2024-11-26 21:10:27

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

get :new, :apikey => "something"

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

assigns[:shop].should_not be_nil

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

According to your routes, :apikey is a required parameter, so you need to add it to your get:

get :new, :apikey => "something"

Also you should change the expectation in your next line to this:

assigns[:shop].should_not be_nil

Use assigns to check the controller's instance variables, and separate should_not from the matcher with a space, not an underscore. That last bit takes some getting used to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文