Day14 测试写起乃-request vs controller test

我们公司的专案在 rails4 所以一般我都还是在写 controller test
一直到某天看到有 request test 才开始好奇到底跟 controller test 差别在哪?

Request test VS Controller test

我们先来看如果是 简单的 Controller test 与 Request test 写法差异

# controller test

require 'rails_helper'

RSpec.describe CollectionsController, type: :controller do
  describe 'Get #index' do
    it 'when resource is found' do
      get :index
      collection = create(:collection)
      expect(response.status).to eq 200
      expect(assigns(:collection)).to eq([collection])
    end
  end

  describe 'Post #create' do
    it 'redirect to index when create collection success' do
      post :create, params: { collection: { title: '11', description: '222' } }
      expect(response).to redirect_to(collections_path)
      expect(Collection.count).to eq 1
    end
  end

  describe 'Get #show' do
    let(:collection) { create(:collection) }
    it 'redirect to index when create collection success' do
      get :show, params: { id: collection }
      expect(response).to have_http_status(200)
      expect(response).to render_template(:show)
    end
  end
end
# request test
require 'rails_helper'

RSpec.describe CollectionsController, type: :request do
  describe 'Get #index' do
    it 'when resource is found' do
      get '/collections'
      expect(response).to have_http_status(200)
      expect(response).to render_template(:index)
    end
  end

  describe 'Post #create' do
    it 'redirect to index when create collection success' do
      post '/collections', params: { collection: { title: '11', description: '222' } }
      expect(response).to redirect_to(collections_path)
      expect(Collection.count).to eq 1
    end
  end

  describe 'Get #show' do
    let(:collection) { create(:collection) }
    it 'redirect to index when create collection success' do
      get "/collections/#{collection.id}"
      expect(response).to have_http_status(200)
      expect(response).to render_template(:show)
    end
  end
end

如果遇到 host 问题 可以看这篇

其实很像只是在於 http 动词後方路径的写法不同而已

额外补充

在 Rails3.5 时释放可以开始使用 Controller spec

但是到了 Rails 5 DHH 开始移除了 assignsassert_templete

Testing what instance variables are set by your controller is a bad idea. That's grossly overstepping the boundaries of what the test should know about. You can test what cookies are set, what HTTP code is returned, how the view looks, or what mutations happened to the DB, but testing the innards of the controller is just not a good idea.

The same goes for assert_template. This ties the test to the internal structure of how your views are organized. That's not what the test should be testing. It should be testing what DOM elements it expect to be there. Rearranging your templates and partials should not cause these tests to break. Deprecate and recommend using assert_select instead.

这边也说到他已经移除了ActionController::TestCase

With the speed increase we achieved for ActionDispatch::IntegrationTest in Rails 5, there's no longer a need to also have ActionController::TestCase around. We're changing the scaffolds in #22076 but we should also get the message out that ActionController::TestCase is deprecated and will be made into a gem from Rails 5.1.

个人理解是鼓励工程师将 Controller 测试改成 Request 测试
所以他才在 Rails5 的时候提升 request spec 的速度

所以你问我到底该选哪个?

==Rails3.5~4 => controller spec==
==Rails5 up => request spec==

因为在 rails5 之前 request 速度确实比 controller 还要慢
在我们自己的专案上 blog test

controller spec

request spec

但我在 rails6 自己的专案上确实 request test 速度有变快! 但其实我自己觉得速度差不多XDD 不过官方都鼓励说要开始转写 request 了就照着官方的来吧!

参考来源有以下:
Deprecate assigns() and assert_template in controller testing
Deprecate ActionController::TestCase in favor of ActionDispatch::IntegrationTest
rspec relish-request spec
各个spec该如何设定host
Testing with RSpec - Request Spec
Controller Specs vs Request Specs?


<<:  IOS、Python自学心得30天 Day-11 模组训练改善-3

>>:  [Day13]PHP 匿名函式及箭头函式

身份验证器或身份验证机制(authenticator or authentication mechanism)

客户端的属性或属性值无法向IdP认证客户端。例如,提交用户名和员工ID的用户将不会向IdP进行身份验...

【把玩Azure DevOps】Day22 建立自管的Azure DevOps Agent(Linux Container agent)

前一篇文章建立了Azure DevOps Agent的Windows Container Image...

CSS微动画 - Transform不一定是位移的最佳选择

Q: 效能跟效果之间怎麽取舍? A: 如果效果不复杂,用一些渲染成本比较高的写法也无妨 新属性搭配...

Day13 React- Forms(1)

小实作React Form 和Event的应用,使用useState Hook,让input输入的值...

如何让网路社团的发文得到较好的转换效果

透过网路社团发文做行销,因为几乎等於零成本,所以一直都是很热门的行销管道,但要得到好的发文转换效果,...