【发布时间】:2016-11-26 22:19:19
【问题描述】:
如果我通过运行 rspec spec/controllers/brands_controller_spec.rb 来运行以下规范,则最后一个测试失败,给出用户不在数据库中的 PG 错误。但是如果我单独运行测试,它们都通过了,所以看起来我的测试并没有搞砸,只是我在 rspec 中缺少的东西?
每次更新用户时,我都会创建一个活动,该活动在创建用户并登录(Devise 跟踪登录日期等)时触发,从而创建一个活动。当我的测试以管理员用户登录时,它尝试使用与管理员用户的关联来创建此活动,错误是说即使我登录了该用户,我也违反了 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
spec/support/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
【问题讨论】:
-
可以肯定的是,
user和admin的工厂工作正常吗?检查创建的用户是否有效。 -
是的,这行得通。如果我单独运行测试,它们是有效的并且两者都有效,这真的很奇怪。只有当我尝试运行文件中的所有测试时,它们才会失败。
-
您是否使用了一些数据库清理器和
before_save回调来跟踪活动(用户创建)? -
我正在使用 after_create、after_update 和 after_destroy 来跟踪模型级别的活动。我正在使用 database_cleaner。我刚刚将该配置添加到问题中以更加清晰。我在那里想念什么?
-
也许这可以帮助你,我确定:stackoverflow.com/questions/2979369/…
标签: ruby-on-rails ruby rspec