1

I am using the hanami framework and the rspec lib to test my code and PG database. When I trying to test one action I get following error:

PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block

My action has been wrapped in repository transaction:

  def call(params, dependencies)
    begin
      dependencies.repository.transaction do
        create_user(params, dependencies)
        assign_tags(params, dependencies)
        assign_notes(params, dependencies)
      end
    rescue Hanami::Model::Error, Domain::Errors::Exception => error
      raise Domain::Errors::CreateUserFailed, error.message
    end
  end

Here you can see part of my test which are failing:

it 'should raise exception if params contains duplicated email' do
expect{service.call(duplicated_email)}
  .to raise_exception(Domain::Errors::CreateUserFailed)
  .with_message('Duplicated values found (id or 
  email)')
end

it 'should raise exception if params contains duplicated note id' do
expect{service.call(duplicated_note_id)}
  .to raise_exception(Domain::Errors::CreateUserFailed)
end

First test pass but the second one raises exception I mentioned earlier. I know this is caused that previous test raised exception in transaction block. I have found solution mentioned in this answer but I don't work in my case.

RSpec.configure do |config|
  config.before(:suite) do
   DatabaseCleaner.clean_with :deletion
  end

  config.before(:each) do
   DatabaseCleaner.strategy = :transaction
  end

  config.before(:each) do
   DatabaseCleaner.start
  end

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

Thanks for all answers:)

EDIT:

 Failure/Error:
   expect{service.call(duplicated_note_id)}
     .to raise_exception(Domain::Errors::CreateUserFailed)
     .with_message('Duplicated values found in user notes (id)')

   expected Domain::Errors::CreateUserFailed with "Duplicated values found in user notes (id)", got #<Domain::Errors::CreateUserFailed: PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
   > with backtrace:
     # ./lib/domain/commands/create_user.rb:17:in `rescue in call'
     # ./lib/domain/commands/create_user.rb:8:in `call'
     # ./spec/domain/commands/create_user_spec.rb:210:in `block (3 levels) in <top (required)>'
     # ./spec/domain/commands/create_user_spec.rb:210:in `block (2 levels) in <top (required)>'
 # ./spec/domain/commands/create_user_spec.rb:210:in `block (2 levels) in <top (required)>'
dewastator
  • 213
  • 4
  • 13
  • Could you provide first few dozens of lines from the backtrace? Although DatabaseCleaner works with Sequel, I'm not sure if it works OOTB with Hanami, it may require more manual setup – Nikita Shilnikov Jun 10 '17 at 22:46
  • at a first look it seems one of your methods create_user/assign_tags/assign_notes issues an invalid SQL statement and for some reason the error doesn't get thrown, instead, the execution continues until it issues another one SQL statement which in order throws the error you see. I'd recommend to replace `raise Domain::Errors::CreateUserFailed, error.message` with `raise Domain::Errors::CreateUserFailed, error.message, e.backtrace` so that you can see the original backtrace. Also, check out the logs for any errors – Nikita Shilnikov Jun 12 '17 at 14:33

0 Answers0