我遇到了使用minitest测试控制器的麻烦。我的问题仅在以下情况 - 当我试图删除记录时。控制器只是命名空间admin
中的基本支架,但测试如下:
require 'test_helper'
describe Admin::ProvidersController do
let(:provider) { FactoryGirl.create(:provider) }
it "gets index" do
get :index
assert_response :success
assert_not_nil assigns(:providers)
end
it "gets new" do
get :new
assert_response :success
end
it "creates provider" do
assert_difference('Provider.count') do
post :create, provider: { description: 'Description', name: 'Name' }
end
assert_redirected_to admin_provider_path(assigns(:provider))
end
it "shows provider" do
get :show, id: provider
assert_response :success
end
it "gets edit" do
get :edit, id: provider
assert_response :success
end
it "updates provider" do
put :update, id: provider, provider: { description: 'New description', name: 'New name' }
assert_redirected_to admin_provider_path(assigns(:provider))
end
it "destroys provider" do
assert_difference('Provider.count', -1, 'An Provider should be destroyed') do
delete :destroy, id: provider
end
assert_redirected_to admin_providers_path
end
end
在rake test:controllers
我看到了:
Started
Admin::ProvidersController
test_0001_gets index PASS (0.12s)
test_0002_gets new PASS (0.17s)
test_0003_creates provider PASS (0.01s)
test_0004_shows provider PASS (0.04s)
test_0005_gets edit PASS (0.05s)
test_0006_updates provider PASS (0.01s)
0
0
test_0007_destroys provider FAIL (0.02s)
Minitest::Assertion: An Provider should be destroyed.
"Provider.count" didn't change by -1.
Expected: -1
Actual: 0
/Users/alec/.rvm/gems/ruby-2.1.1@lorgnette/gems/activesupport-4.1.1/lib/active_support/testing/assertions.rb:76:in `block in assert_difference'
/Users/alec/.rvm/gems/ruby-2.1.1@lorgnette/gems/activesupport-4.1.1/lib/active_support/testing/assertions.rb:73:in `each'
/Users/alec/.rvm/gems/ruby-2.1.1@lorgnette/gems/activesupport-4.1.1/lib/active_support/testing/assertions.rb:73:in `each_with_index'
/Users/alec/.rvm/gems/ruby-2.1.1@lorgnette/gems/activesupport-4.1.1/lib/active_support/testing/assertions.rb:73:in `assert_difference'
/Users/alec/Documents/Projects/RubyApps/lorgnette/test/controllers/admin/providers_controller_test.rb:42:in `block (2 levels) in <top (required)>'
Fabulous run in 0.43316s
7 tests, 11 assertions, 1 failures, 0 errors, 0 skips
我在日志中看到:
-------------------------------------------------------
Admin::ProvidersController: test_0007_destroys provider
-------------------------------------------------------
(0.4ms) SELECT COUNT(*) FROM "providers"
(0.3ms) SELECT COUNT(*) FROM "providers"
(0.2ms) SAVEPOINT active_record_1
SQL (0.4ms) INSERT INTO "providers" ("created_at", "description", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", "2014-05-30 23:06:43.102303"], ["description", "MyText"], ["name", "MyString"], ["updated_at", "2014-05-30 23:06:43.102303"]]
(0.2ms) RELEASE SAVEPOINT active_record_1
Processing by Admin::ProvidersController#destroy as HTML
Parameters: {"id"=>"271"}
Provider Load (0.4ms) SELECT "providers".* FROM "providers" WHERE "providers"."id" = $1 LIMIT 1 [["id", 271]]
(0.2ms) SAVEPOINT active_record_1
SQL (0.3ms) DELETE FROM "providers" WHERE "providers"."id" = $1 [["id", 271]]
(0.1ms) RELEASE SAVEPOINT active_record_1
Redirected to http://test.host/admin/providers
Completed 302 Found in 3ms (ActiveRecord: 0.9ms)
(0.3ms) SELECT COUNT(*) FROM "providers"
(0.2ms) SELECT COUNT(*) FROM "providers"
(0.5ms) ROLLBACK
删除测试有什么问题?
答案 0 :(得分:3)
是的,因为在assert_difference提供者计数为零时,当引用“id:provider”时,提供者计数会递增,然后提供者计数会被destroy递减。所以它从零开始,到零结束。
试试这个......
it "destroys provider" do
provider
assert_difference('Provider.count', -1, 'An Provider should be destroyed') do
delete :destroy, id: provider
end
end
这样,提供程序在assert_difference块之前被实例化。
答案 1 :(得分:2)
史蒂夫说,在你拨打provider
电话之前,尚未创建delete
。您可以使用let!
instead of let
轻松避免这种情况。
let!(:provider) { FactoryGirl.create(:provider) }
使用let!
会在测试开始之前调用FactoryGirl.create
。