我一直在处理一组故障,其中启用VCR的测试会在我的Projects表中添加一条额外的记录,并且该记录会在其他测试中持续存在,从而导致失败,而不是在结束时回滚考试。具体来说,我有4个由我的Rails灯具创建的项目,然后是一个包含VCR卡带的测试,以添加一个额外的项目。该测试总是通过,但数据库之后不会重置为4个项目。
方便的是,该项目是开源的,因此您可以自己运行它。可以在https://github.com/linuxfoundation/cii-best-practices-badge的master上看到该错误(checkout commit 048776f,以便不获得临时解决方法)。导致问题的测试是https://github.com/linuxfoundation/cii-best-practices-badge/blob/master/test/features/github_login_test.rb(使用VCR磁带)登录到Github并从测试用户获取公共项目列表。它通过https://github.com/blowmage/minitest-rails-capybara gem使用Capybara。
使用SEED=41779 rake test
运行时,使用VCR的测试会成功,但稍后的测试会失败,因为它会计算5个而不是4个项目。
以下是测试日志,您可以看到最后调用了ROLLBACK
,但在测试开始时从未指定过SAVEPOINT
。
---------------------------------------------------
GithubLoginTest: test_0001_Has link to GitHub Login
---------------------------------------------------
Started GET "/" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by StaticPagesController#home as HTML
Rendered static_pages/home.html.erb within layouts/application (0.2ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (1.3ms)
Completed 200 OK in 6ms (Views: 4.7ms | ActiveRecord: 0.0ms)
Started GET "/projects/new" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by ProjectsController#new as HTML
Rendered sessions/new.html.erb (3.2ms)
Rendered projects/new.html.erb within layouts/application (8.6ms)
Rendered layouts/_header.html.erb (1.5ms)
Rendered layouts/_footer.html.erb (1.9ms)
Completed 200 OK in 28ms (Views: 25.0ms | ActiveRecord: 0.0ms)
Started POST "/auth/github" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Started GET "/auth/github/callback" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by SessionsController#create as HTML
Parameters: {"provider"=>"github"}
[1m[36mUser Load (0.3ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."provider" = $1 AND "users"."uid" = $2 LIMIT 1[0m [["provider", "github"], ["uid", "12345"]]
[1m[35m (0.1ms)[0m BEGIN
[1m[36mSQL (0.5ms)[0m [1mINSERT INTO "users" ("provider", "uid", "name", "email", "nickname", "activated", "created_at", "updated_at", "activation_digest") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING "id"[0m [["provider", "github"], ["uid", "12345"], ["name", "CII Test"], ["email", "test@example.com"], ["nickname", "CIITheRobot"], ["activated", "t"], ["created_at", "2016-05-31 02:02:03.288717"], ["updated_at", "2016-05-31 02:02:03.288717"], ["activation_digest", "$2a$04$BhFbVTS54Yeeurh83VBmdOs.T30jvliDmEFIQ6/lMpnhpN7rGvVTW"]]
[1m[35m (0.3ms)[0m COMMIT
Redirected to http://127.0.0.1:31337/
[1m[36mUser Load (0.3ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 764040364]]
Completed 302 Found in 11ms (ActiveRecord: 1.5ms)
Started GET "/" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by StaticPagesController#home as HTML
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 764040364]]
Rendered static_pages/home.html.erb within layouts/application (0.2ms)
Rendered layouts/_header.html.erb (0.6ms)
Rendered layouts/_footer.html.erb (1.2ms)
Completed 200 OK in 6ms (Views: 4.3ms | ActiveRecord: 0.2ms)
Started GET "/projects/new" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by ProjectsController#new as HTML
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 764040364]]
Rendered shared/_error_messages.html.erb (0.6ms)
Rendered projects/new.html.erb within layouts/application (31.6ms)
Rendered layouts/_header.html.erb (1.0ms)
Rendered layouts/_footer.html.erb (1.8ms)
Completed 200 OK in 41ms (Views: 38.6ms | ActiveRecord: 0.2ms)
Started POST "/projects" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "project"=>{"repo_url"=>"https://github.com/ciitest/cii-best-practices-badge"}, "commit"=>"Submit GitHub Repository"}
[1m[35mUser Load (0.4ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 764040364]]
[1m[36mProject Exists (0.6ms)[0m [1mSELECT 1 AS one FROM "projects" WHERE "projects"."repo_url" = $1 LIMIT 1[0m [["repo_url", "https://github.com/ciitest/cii-best-practices-badge"]]
[1m[35m (0.2ms)[0m BEGIN
[1m[36mProject Exists (0.6ms)[0m [1mSELECT 1 AS one FROM "projects" WHERE "projects"."repo_url" = 'https://github.com/ciitest/cii-best-practices-badge' LIMIT 1[0m
[1m[35mSQL (1.0ms)[0m INSERT INTO "projects" ("repo_url", "user_id", "homepage_url", "name", "sites_https_status", "sites_https_justification", "repo_public_status", "repo_public_justification", "repo_track_status", "repo_track_justification", "repo_distributed_status", "repo_distributed_justification", "contribution_status", "contribution_justification", "discussion_status", "discussion_justification", "license_location_status", "license_location_justification", "release_notes_status", "release_notes_justification", "build_status", "build_justification", "build_common_tools_status", "build_common_tools_justification", "badge_percentage", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27) RETURNING "id" [["repo_url", "https://github.com/ciitest/cii-best-practices-badge"], ["user_id", 764040364], ["homepage_url", "https://bestpractices.coreinfrastructure.org"], ["name", "Core Infrastructure Initiative Best Practices Badge"], ["sites_https_status", "Met"], ["sites_https_justification", "Given only https: URLs."], ["repo_public_status", "Met"], ["repo_public_justification", "Repository on GitHub, which provides public git repositories with URLs."], ["repo_track_status", "Met"], ["repo_track_justification", "Repository on GitHub, which uses git. git can track the changes, who made them, and when they were made."], ["repo_distributed_status", "Met"], ["repo_distributed_justification", "Repository on GitHub, which uses git. git is distributed."], ["contribution_status", "Met"], ["contribution_justification", "Non-trivial contribution file in repository: <https://github.com/ciitest/cii-best-practices-badge/blob/master/CONTRIBUTING.md>."], ["discussion_status", "Met"], ["discussion_justification", "GitHub supports discussions on issues and pull requests."], ["license_location_status", "Met"], ["license_location_justification", "Non-trivial license location file in repository: <https://github.com/ciitest/cii-best-practices-badge/blob/master/LICENSE>."], ["release_notes_status", "Met"], ["release_notes_justification", "Non-trivial release notes file in repository: <https://github.com/ciitest/cii-best-practices-badge/blob/master/CHANGELOG.md>."], ["build_status", "Met"], ["build_justification", "Non-trivial build file in repository: <https://github.com/ciitest/cii-best-practices-badge/blob/master/Rakefile>."], ["build_common_tools_status", "Met"], ["build_common_tools_justification", "Non-trivial build file in repository: <https://github.com/ciitest/cii-best-practices-badge/blob/master/Rakefile>."], ["badge_percentage", 15], ["created_at", "2016-05-31 02:02:03.602250"], ["updated_at", "2016-05-31 02:02:03.602250"]]
[1m[36mSQL (0.4ms)[0m [1mINSERT INTO "versions" ("event", "whodunnit", "created_at", "item_id", "item_type") VALUES ($1, $2, $3, $4, $5) RETURNING "id"[0m [["event", "create"], ["whodunnit", "764040364"], ["created_at", "2016-05-31 02:02:03.602250"], ["item_id", 980190963], ["item_type", "Project"]]
[1m[35m (0.5ms)[0m COMMIT
Redirected to http://127.0.0.1:31337/projects/980190963/edit
Completed 302 Found in 117ms (ActiveRecord: 4.3ms)
Started GET "/projects/980190963/edit" for 127.0.0.1 at 2016-05-30 22:02:03 -0400
Processing by ProjectsController#edit as HTML
Parameters: {"id"=>"980190963"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 764040364]]
[1m[35mProject Load (0.7ms)[0m SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 ORDER BY "projects"."created_at" ASC LIMIT 1 [["id", 980190963]]
[1m[36mProject Exists (0.3ms)[0m [1mSELECT 1 AS one FROM "projects" WHERE "projects"."user_id" = $1 AND "projects"."id" = $2 LIMIT 1[0m [["user_id", 764040364], ["id", 980190963]]
Rendered projects/_details.html.erb (0.0ms)
Rendered shared/_error_messages.html.erb (0.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (3.3ms)
Rendered projects/_status_chooser.html.erb (2.4ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (3.9ms)
Rendered projects/_status_chooser.html.erb (3.3ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (3.2ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.4ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.7ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (2.8ms)
Rendered projects/_status_chooser.html.erb (2.9ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.6ms)
Rendered projects/_status_chooser.html.erb (2.7ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_status_chooser.html.erb (2.1ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.1ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_status_chooser.html.erb (1.9ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.4ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (3.1ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (4.6ms)
Rendered projects/_status_chooser.html.erb (3.6ms)
Rendered projects/_status_chooser.html.erb (3.1ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.6ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_status_chooser.html.erb (2.1ms)
Rendered projects/_status_chooser.html.erb (3.6ms)
Rendered projects/_status_chooser.html.erb (3.7ms)
Rendered projects/_status_chooser.html.erb (3.6ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (3.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.7ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_status_chooser.html.erb (2.1ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (3.9ms)
Rendered projects/_status_chooser.html.erb (2.7ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (4.0ms)
Rendered projects/_status_chooser.html.erb (2.8ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.5ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_status_chooser.html.erb (2.0ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.1ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.6ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.4ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.2ms)
Rendered projects/_details.html.erb (0.0ms)
Rendered projects/_status_chooser.html.erb (2.3ms)
Rendered projects/_form.html.erb (202.1ms)
Rendered projects/edit.html.erb within layouts/application (202.6ms)
Rendered layouts/_header.html.erb (0.7ms)
Rendered layouts/_footer.html.erb (1.3ms)
Completed 200 OK in 214ms (Views: 207.2ms | ActiveRecord: 1.1ms)
Started GET "/users/764040364" for 127.0.0.1 at 2016-05-30 22:02:04 -0400
Processing by UsersController#show as HTML
Parameters: {"id"=>"764040364"}
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 764040364]]
[1m[36mUser Load (0.7ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 764040364]]
[1m[35mProject Load (3.6ms)[0m SELECT "projects".* FROM "projects" WHERE "projects"."repo_url" IN ('https://github.com/ciitest/cii-best-practices-badge', 'https://github.com/ciitest/test-repo') ORDER BY "projects"."created_at" ASC
[1m[36mProject Load (1.1ms)[0m [1mSELECT "projects".* FROM "projects" WHERE "projects"."user_id" = $1 ORDER BY "projects"."created_at" ASC LIMIT 30 OFFSET 0[0m [["user_id", 764040364]]
[1m[35mProject Exists (0.3ms)[0m SELECT 1 AS one FROM "projects" WHERE "projects"."user_id" = $1 LIMIT 1 [["user_id", 764040364]]
Rendered projects/_table.html.erb (2.3ms)
Rendered users/show.html.erb within layouts/application (6.9ms)
Rendered layouts/_header.html.erb (1.1ms)
Rendered layouts/_footer.html.erb (1.1ms)
Completed 200 OK in 48ms (Views: 13.5ms | ActiveRecord: 5.9ms)
Started GET "/projects/980190963/badge" for 127.0.0.1 at 2016-05-30 22:02:04 -0400
Processing by ProjectsController#badge as SVG
Parameters: {"id"=>"980190963"}
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", 764040364]]
[1m[35mProject Load (0.8ms)[0m SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 ORDER BY "projects"."created_at" ASC LIMIT 1 [["id", 980190963]]
Rendered text template (0.0ms)
Sent data (0.7ms)
Completed 200 OK in 8ms (Views: 0.6ms | ActiveRecord: 1.0ms)
[1m[36m (0.3ms)[0m [1mROLLBACK[0m
[1m[35m (0.1ms)[0m BEGIN
----------------------------------------------
将此与通常设置SAVEPOINT
和ROLLBACK
的后续测试进行对比:
--------------------------------------------------------
ProjectStatTest: test_project_stat_matches_expect_values
--------------------------------------------------------
[1m[36m (0.0ms)[0m [1mSAVEPOINT active_record_1[0m
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 0)
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 25)[0m
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 50)
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 75)[0m
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 90)
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "projects" WHERE ("projects"."badge_percentage" >= 100)[0m
[1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "projects" WHERE ("projects"."created_at" >= '2015-02-28 12:00:00.000000')
[1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "projects" WHERE ("projects"."updated_at" >= '2015-02-28 12:00:00.000000')[0m
[1m[35mSQL (0.0ms)[0m INSERT INTO "project_stats" ("created_at", "updated_at", "percent_ge_0", "percent_ge_25", "percent_ge_50", "percent_ge_75", "percent_ge_90", "percent_ge_100", "created_since_yesterday", "updated_since_yesterday") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id" [["created_at", "2015-03-01 12:00:00.000000"], ["updated_at", "2015-03-01 12:00:00.000000"], ["percent_ge_0", 5], ["percent_ge_25", 2], ["percent_ge_50", 2], ["percent_ge_75", 2], ["percent_ge_90", 1], ["percent_ge_100", 1], ["created_since_yesterday", 5], ["updated_since_yesterday", 0]]
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
[1m[35m (0.1ms)[0m ROLLBACK
[1m[36m (0.1ms)[0m [1mBEGIN[0m
--------------------------------------------
如何设置SAVEPOINT
以使ROLLBACK
成功?我怀疑我遇到了VCR的错误(我在https://github.com/vcr/vcr/issues/586提交了),但很难确定。
答案 0 :(得分:0)
从快速浏览一下,看起来您正在使用标记为js:true的测试进行事务测试 - 这不起作用,因为当使用支持JS Capybara的驱动程序加载正在单独线程中测试的应用程序时。然后每个线程(测试和应用程序)都有自己的数据库连接,并且在一个线程中启动的事务对另一个线程是不可见的,直到它被提交(由于事务测试可能永远不会发生) )。这也意味着在应用程序中创建的记录不会被清理,因为它们实际上并未在回滚的测试事务中创建。您可能希望查看类似DatabaseCleaner的内容,以便与您的js:true标记测试一起使用 - https://github.com/DatabaseCleaner/database_cleaner#minitest-example和https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example的混合是一个很好的起点