2016-11-26 2 views
0

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' 

編集: は、私がテスト内の詮索を使用した別のエラーを追加します。

実際にユーザーを作成するには、2番目のテストで行われるユーザー作成をPGで防ぎます。データベースクリーナーのstuffsに追加

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 
+0

「user」と「admin」の工場は確実に機能しますか?作成したユーザーが有効かどうかを確認します。 –

+0

うん、それは動作します。それらは有効であり、もし私が実際に奇妙なテストを個別に実行すると、どちらも動作します。失敗したファイル内のすべてのテストを実行しようとするときだけです。 –

+0

データベースクリーナーと 'before_save'コールバックを使ってアクティビティ(ユーザ作成)を追跡していますか? –

答えて

1

:truncationに戦略を変更してみてください。

関連する問題