【问题标题】:RSpec put, post, get, delete, patch behave the same wayRSpec put、post、get、delete、patch 的行为方式相同
【发布时间】:2015-09-08 07:40:41
【问题描述】:

假设我这样指定资源路由:
resources :projects, only: [:index, :show, :create, :update, :destroy]

通常在控制器测试中,我们只需执行以下操作即可触发:create 的操作:

post(:create)

当我们使用delete(:create) 执行此操作时,它应该抛出routing error 异常。但事实并非如此。它也适用于get(:create)put(:create)patch(:create)

这种行为的解释是什么?这对于控制器规格是否正常?

【问题讨论】:

    标签: ruby-on-rails-4 rspec rspec3


    【解决方案1】:

    这是控制器规范的预期行为。方法getpostpatchputdeletehead 定义在ActionController::TestCase 中。每个方法都模拟各自的 HTTP 请求,并调用正在测试的控制器中的操作。例如:

    post(:create)
    

    模拟 POST 并通过调用 create 操作来处理它。您可能还有其他需要 POST 的操作;要测试它们,您可以将 :create 替换为操作名称。

    请注意,不涉及路由器。路由不是控制器的责任,因此控制器规范不会测试路由器的行为。要为路由编写单元测试,请使用routing specs

    调用delete(:create) 的意思是“模拟DELETE 并使用create 操作处理它”。这没有多大意义。但是,没有什么能阻止您创建将DELETE /resource/:id 发送到create 操作的路由,如果这是您想要的(注意:这可能不是您想要的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-17
      • 2020-04-11
      • 1970-01-01
      • 2021-09-09
      • 2019-07-18
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多