FactoryGirl创建的记录在测试期间不在数据库中

时间:2014-07-31 07:19:02

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

我尝试将用户保存到数据库并测试我可以使用以下规范查看用户的个人资料页面:

require 'spec_helper'

describe "User pages" do

  subject { page }

  let(:user) { create(:user) }
  before { sign_in user }

  describe "profile page" do
    before { visit '/#/user' }

    it { should have_content("Name") }
    it { should have_content("Email address") }
    it { should have_content("Mobile phone") }
    it { current_path.should end_with('#/user') }
  end
end

这是我的用户工厂:

FactoryGirl.define do

  factory :service_company do
    name     "Service Company Test"
  end

  factory :yard do
    name     "Yard Test"
  end

  factory :service_company_account do
    association :yard, factory: :yard
    association :service_company, factory: :service_company
  end

  factory :user do
    name     "Pawan Raj"
    email    "pawan@example.com"
    phone    "5125552343"
    password "foobar"
    password_confirmation "foobar"

    association :service_company_account, factory: :service_company_account
  end
end

在我的用户控制器中,get_logged_in_user操作(在上面的测试中调用),即使在创建后,Users表也始终为空,我无法理解原因。当我查看test.log文件时,数据库似乎说用户已经存在,但是它似乎无论如何都要插入新用户。所以这种或那种方式,我认为用户应该在create()调用之后存在。我可能对FactoryGirl应该如何工作有一些基本的误解:

D, [2014-07-31T02:04:12.784184 #37634] DEBUG -- :   [1m[35mUser Exists (0.3ms)[0m  SELECT  1 AS one FROM "users"  WHERE LOWER("users"."email") = LOWER('pawan@example.com') LIMIT 1
D, [2014-07-30T18:30:41.624291 #37146] DEBUG -- :   [1m[35mSQL (0.5ms)[0m  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "phone", "remember_token", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["created_at", "2014-07-30 23:30:41.622815"], ["email", "pawan@example.com"], ["name", "Pawan Raj"], ["password_digest", "$2a$04$VbyWKSjHxAv1C1K7ZGqIh.RZzO/wbLhimQuLrdjVPTcStoGuFcKra"], ["phone", "5125552343"], ["remember_token", "529ae9a893f14df202410268095dde8e64670980"], ["updated_at", "2014-07-30 23:30:41.622815"]]
D, [2014-07-31T02:04:12.788341 #37634] DEBUG -- :   [1m[35mSQL (0.3ms)[0m  UPDATE "service_company_accounts" SET "updated_at" = $1, "user_id" = $2 WHERE "service_company_accounts"."id" = 302  [["updated_at", "2014-07-31 07:04:12.785914"], ["user_id", 2848]]
D, [2014-07-31T02:04:12.788696 #37634] DEBUG -- :   [1m[36m (0.1ms)[0m  [1mRELEASE SAVEPOINT active_record_1[0m

另外,对于记录,当我手动使用网站时,这一切都可以正常工作。所以这是一个测试问题。

编辑:这是我的spec_helper.rb文件的全部内容,以防它有助于了解情况:

require 'rubygems'
require 'spork'

Spork.prefork do
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'capybara/rspec'
  require 'capybara/angular'
  require 'capybara/poltergeist'
  require 'rspec/rails'
  require 'rspec/autorun'
  require 'mailchimp'
  require 'paperclip/matchers'

  Capybara.javascript_driver = :poltergeist
  Capybara.default_driver = :poltergeist

  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

  ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

  RSpec.configure do |config|
    config.fixture_path = "#{::Rails.root}/spec/fixtures"
    config.use_transactional_fixtures = true
    config.infer_base_class_for_anonymous_controllers = false
    config.order = "random"
    config.include Capybara::Angular::DSL
    config.include Paperclip::Shoulda::Matchers
  end
end

0 个答案:

没有答案