Rails5 controller testing

Rails5 還在 beta 當(dāng)中,但是即將來(lái)臨的版本升級(jí)有相當(dāng)多的改變(列如新功能 ActionCable),其中之一是針對(duì) controller testing. 在 Rails5 升級(jí)之下,controller test 改為繼承 ActionDispatch::IntegrationTest, 而且ActionDispatch::IntegrationTest 并沒(méi)有 ActionController::TestCaseassigns() 方式。

在很多現(xiàn)成項(xiàng)目環(huán)境之下,要開(kāi)始測(cè)試 Rails5 升級(jí),并且同時(shí)保持現(xiàn)有的測(cè)試庫(kù)必須使用 rails-controller-testing gem.

Gemfile:
group :test do 
 gem “rails-controller-testing”
end

Rails5 關(guān)于controller testing 語(yǔ)法有改變,除了 get 請(qǐng)求意外必須指定 controller action, 及 request method.

Rails 4.2
test “should show world” do 
  get :show, id: @world
  assert_response :success
end

test “should update world” do 
  patch :update, id: @world, world: {title: “hello”}
  assert_redirected_to world_path(assigns(:world))
end
rails 5.0.0.beta1

除了 get 以外,其他 controller action 必須改換 process,同時(shí)注意params 的用法改變。

test “should show world” do 
  get :show, params: {id: @world}
  assert_response :success
end

test “should update world” do 
   process :update, method: :patch, params: {id: @world, world: {title: “hello”}}
   assert_redirected_to world_path(assigns(:world))
end

References:

Comment
本文章寫(xiě)作時(shí)的 rails 版本為 5.0.0.beta1.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容