FactoryGirl creates user, but save point is released before test starts

瘦欲@ 提交于 2020-01-23 01:55:11

问题


I am running rspec tests for spec/requests/user_pages_specs:

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

As you can see, I am using FactoryGirl to create a user. When I run the same command (i.e. FactoryGirl.create(:user)) is rails console test --sandbox, a user is properly created and saved.

When I run my tests however, FactoryGirl creates the user in the database, and then releases it before the test actually runs. I can see this from 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"]]

Why is this happening and how do I stop it from releasing until after the tests?

来源:https://stackoverflow.com/questions/23250739/factorygirl-creates-user-but-save-point-is-released-before-test-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!