FactoryGirl创建用户,但在测试开始之前释放保存点

时间:2014-04-23 16:55:53

标签: ruby-on-rails rspec factory-bot

我正在为spec / requests / user_pages_specs运行rspec测试:

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "home page" do
    before { visit root_path }

    it { should have_content('Sign up with Facebook') }
    it { should have_title(full_title('')) }
  end

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user.id) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end
end

如您所见,我正在使用FactoryGirl来创建用户。当我运行相同的命令(即FactoryGirl.create(:user))为rails console test --sandbox时,将正确创建并保存用户。

但是,当我运行测试时,FactoryGirl会在数据库中创建用户,然后在测试实际运行之前将其释放。我可以从test.log看到这个:

[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m  [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
  [1m[35m (0.1ms)[0m  begin transaction
  [1m[36m (0.1ms)[0m  [1mSAVEPOINT active_record_1[0m
  [1m[35mUser Exists (0.1ms)[0m  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('test@foobar.com') LIMIT 1
  [1m[36mSQL (2.0ms)[0m  [1mINSERT INTO "users" ("created_at", "email", "name", "oauth_expires_at", "oauth_token", "provider", "uid", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)[0m  [["created_at", Wed, 23 Apr 2014 16:17:16 UTC +00:00], ["email", "test@foobar.com"], ["name", "Test User"], ["oauth_expires_at", Wed, 23 Apr 2014 00:00:00 UTC +00:00], ["oauth_token", "10987654321ABCDEFG"], ["provider", "facebook"], ["uid", "12345"], ["updated_at", Wed, 23 Apr 2014 16:17:16 UTC +00:00]]
  [1m[35m (0.0ms)[0m  RELEASE SAVEPOINT active_record_1
Started GET "/users/1" for 127.0.0.1 at 2014-04-23 10:17:16 -0600
Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
  [1m[36mUser Load (0.1ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1[0m  [["id", "1"]]

为什么会发生这种情况?如何在测试之后阻止它发布?

0 个答案:

没有答案