簡體   English   中英

Rspec控制器-當我運行單個測試而不是文件中的所有測試時可以工作嗎?

[英]Rspec Controllers - works when I run individual test, but not all tests in file?

如果我通過運行rspec spec / controllers / brands_controller_spec.rb來運行以下規范,則最后一次測試將失敗,並向我顯示用戶不在數據庫中的PG錯誤。 但是,如果我分別運行測試,它們全部通過,那么看來我的測試似乎沒有搞砸,只是rspec缺少了什么?

每當用戶更新時,我都會創建一個活動,該活動在創建用戶並登錄(Devise跟蹤登錄日期等)時觸發,從而創建了活動。 當我的測試登錄到admin用戶時,它嘗試通過與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

嘗試將策略更改為:truncation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM