Rails 5 获取 users_path(10) 测试不会失败,即使控制器中没有定义"show"方法



我增益,是的。有一个奇怪的问题。此测试应该失败,但事实并非如此。

仅使用 Rails 5 --api。

有一个用户控制器,如下所示:

class UsersController < ApplicationController
  def index
    users = User.all
    render json: users
  end
end

测试文件:

require 'test_helper'
class UsersControllerTest < ActionDispatch::IntegrationTest
  test "users should return list of users" do
    get users_path
    assert_response :success
    assert_not response.body.empty?
  end
  test "show valid id should return user json" do
    get users_path(10)
    assert_response :success
  end
end

Routes.rb定义如下:

Rails.application.routes.draw do
  resources :cats
  resources :users
end

控制台输出:

Running via Spring preloader in process 4176
Run options: --seed 28710
# Running:
........
Finished in 0.100764s, 79.3937 runs/s, 138.9390 assertions/s.
8 runs, 14 assertions, 0 failures, 0 errors, 0 skips

当控制器明显没有定义show方法时,为什么测试不报告错误?

转到我的浏览器 localhost:3000/users/10 返回 JSON,表示"show"方法不存在。

有什么想法吗?

将测试中的users_path(10)替换为user_path(10)

最新更新