&#34;未定义的方法`deleted_at =&#39;对于#<spree :: user“,even =”“but =”“this =”“column =”“exists =”“

时间:2016-01-25 08:54:19

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

=”“

当我在测试中执行create(:user)时,我收到此错误:undefined method deleted_at=' for #<Spree::User。 它发生在我尝试create(:user)的所有测试中。

当我投入一个byebug时会发生这种情况:

$ rspec ./spec/models/spree/permission_sets/restricted_stock_transfer_display_spec.rb:40

  1) Spree::PermissionSets::RestrictedStockTransferDisplay when not activated
     Failure/Error: let(:user) { create :user }
     ActiveRecord::StatementInvalid:
       PG::UndefinedColumn: ERROR:  column spree_users.deleted_at does not exist
       LINE 1: ..."spree_users"."email" = 'darby@tremblay.name' AND "spree_use...
                                                                    ^
       : SELECT  1 AS one FROM "spree_users" WHERE ("spree_users"."email" = 'darby@tremblay.name' AND "spree_users"."deleted_at" IS NULL) LIMIT 1

user_factory.rb

require 'spree/testing_support/sequences'
require 'spree/testing_support/factories/role_factory'
require 'spree/testing_support/factories/address_factory'

FactoryGirl.define do

  factory :user, class: Spree.user_class do
    email { generate(:random_email) }
    login { email }
    password 'secret'
    password_confirmation { password }
    authentication_token { generate(:user_authentication_token) } if Spree.user_class.attribute_method? :authentication_token


     # [cut...]    

  end
end

deleted_at出现在数据库中

$ psql Solidus_test
psql (9.4.5)

Solidus_test=# \d spree_users

Table "public.spree_users"
             Column         |            Type             |                        Modifiers
    ------------------------+-----------------------------+----------------------------------------------------------

     deleted_at             | timestamp without time zone |

但不在Spree :: User.column_names

如果我在byebug之前在测试中投入create(:user)我发现了这个:

(byebug) Spree::User.column_names

["id", "encrypted_password", .....]

^否&#34; deleted_at&#34;

有关如何实现这一目标的任何提示?为什么我可以看到&#39; deleted_at&#39;使用psql但不能使用Spree::User.column_names

是的,我正在连接到Solidus_test:

$ grep 'test'  config/database.yml -A2
test:
  <<: *default
  database: Solidus_test

检查rspec循环外的测试环境显示存在deleted_at

✗ RAILS_ENV=test r c
Running via Spring preloader in process 4092
Loading test environment (Rails 4.2.5)
irb: warn: can't alias context from irb_context.
2.2.3 :001 > Spree::User.column_names
 => ["id", "encrypted_password", "password_salt", "email", "remember_token", "persistence_token", 
"reset_password_token", "perishable_token", "sign_in_count", "failed_attempts", "last_request_at", 
"current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "login", 
"ship_address_id", "bill_address_id", "authentication_token", "unlock_token", "locked_at", 
"reset_password_sent_at", "created_at", "updated_at", "spree_api_key", "remember_created_at", 
"deleted_at", "confirmation_token", "confirmed_at", "confirmation_sent_at"]

1 个答案:

答案 0 :(得分:0)

当我从solidus gem复制spec/spec_helper时,我忘了更新某些内容。

我通过加载正确的环境修复了spree_users.deleted_at错误:

require File.expand_path("../dummy/config/environment", __FILE__) 22 

应该是:

require File.expand_path("../../config/environment", __FILE__)

感谢所有建议,伙计们! :-D