Rspec控制器 - 当我运行单独的测试时,但不是文件中的所有测试?

时间:2016-11-26 22:19:19

标签: ruby-on-rails ruby rspec

如果我通过运行rspec spec / controllers / brands_controller_spec.rb运行以下规范,则最后一次测试无法向我提供用户不在数据库中的PG错误。但是,如果我单独运行测试,它们都会通过,所以看起来我的测试看起来不是很混乱,只是我对rspec缺少的东西?

我每次更新用户时都会创建一个活动,这是在创建用户并登录(Devise跟踪登录日期等)时触发的,这会创建一个活动。当我的测试登录管理员用户时,它尝试使用与admin用户的关联创建此活动,错误是说即使我登录用户我违反了PG规则,因为用户不在数据库中。 ..即使它是?很困惑。

品牌控制器规格

describe 'GET #index' do

    context 'unauthenticated_user' do
        it "blocks an unauthenticated_user from getting to the index page" do
            get :index
            expect(response).to redirect_to new_user_session_path
        end
    end

    context 'authenticated_user NOT admin' do

        it "redirects non admin employees" do
            login_user
            get :index
            expect(response).to redirect_to @user
        end
    end

    context 'authenticated_user admin' do

        it "renders the index template for the authenticated_user" do
            login_admin
            get :index
            expect(response).to render_template :index
        end
    end                 
end

规格/支持/ controller_macros.rb

module ControllerMacros
  def login_user
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @user = FactoryGirl.create(:user)
      sign_in @user
  end

  def login_admin
      @request.env["devise.mapping"] = Devise.mappings[:user]
      @admin_user = FactoryGirl.create(:admin)
      sign_in @admin_user
  end
end

错误:

1) BrandsController GET #index authenticated_user admin renders the index template for the authenticated_user
     Failure/Error: Activity.create(user_id: user_id, trackable_id: self.id, trackable_type: self.class.name, action: "#{self.class.name} #{self.id} #{self.full_name} created")


 ActiveRecord::InvalidForeignKey:
   PG::ForeignKeyViolation: ERROR:  insert or update on table "activities" violates foreign key constraint "fk_rails_7e11bb717f"
   DETAIL:  Key (user_id)=(2185) is not present in table "users".
   : INSERT INTO "activities" ("user_id", "trackable_id", "trackable_type", "action", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
 # ./app/models/user.rb:691:in `record_create'
 # ./spec/support/controller_macros.rb:10:in `login_admin'
 # ./spec/controllers/brands_controller_spec.rb:26:in `block (4 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # PG::ForeignKeyViolation:
 #   ERROR:  insert or update on table "activities" violates foreign key constraint "fk_rails_7e11bb717f"
 #   DETAIL:  Key (user_id)=(2185) is not present in table "users".
 #   ./app/models/user.rb:691:in `record_create'

编辑: 添加我在测试中使用Pry发现的另一个错误。

PG似乎阻止用户创建在第二次测试中实际创建用户。

ActiveRecord::StatementInvalid: 
  PG::InFailedSqlTransaction: ERROR:      
    current transaction is aborted, commands ignored until end of   transaction block
    : SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1

编辑在数据库清理器中添加内容:

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with :truncation
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
     example.run
    end
 end

 config.after(:each) do
   DatabaseCleaner.clean
 end

1 个答案:

答案 0 :(得分:1)

尝试将策略更改为:truncation

相关问题