Rails5 还在 beta 当中,但是即将来临的版本升级有相当多的改变(列如新功能 ActionCable
),其中之一是针对 controller testing
. 在 Rails5 升级之下,controller test 改为继承 ActionDispatch::IntegrationTest
, 而且ActionDispatch::IntegrationTest
并没有 ActionController::TestCase
的 assigns()
方式。
在很多现成项目环境之下,要开始测试 Rails5 升级,并且同时保持现有的测试库必须使用 rails-controller-testing
gem.
Gemfile:
group :test do
gem “rails-controller-testing”
end
Rails5 关于controller testing 语法有改变,除了 get 请求意外必须指定 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
,同时注意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
本文章写作时的 rails
版本为 5.0.0.beta1
.
网友评论