0

The following scenarios produce the expected results:

  1. from zsh, execute:

    /usr/local/bin/pg_dump mydatabase  #=> a bunch of sql output
    
  2. from irb or rails console, execute:

    `/usr/local/bin/pg_dump my_database` #=> a bunch of sql output
    
  3. Call the same shell command from within a method:

    class AppDb
      def contents
        `/usr/local/bin/pg_dump my_database`
      end
    end
    
    # from rails console:
    AppDb.new.contents #=> a bunch of sql stuff
    

However, when I test the very same AppDb class from Rspec, the shell command hangs indefinitely at the assertion:

expect(AppDb.new.contents).to match "PostgreSQL database dump complete"

Do you have any idea why this might be?

2
  • Probably you need to try this solution stackoverflow.com/questions/11349270/… Commented May 8, 2017 at 20:38
  • @AlexKojin thanks, the problem, however, is not with capturing stdout, that works fine. e.g. if I replace the pg_dump command with pwd, the result is properly captured and tested by rspec. The problem is that pg_dump is hanging (only in rspec context) and there's no diagnostic info available. Commented May 8, 2017 at 23:38

1 Answer 1

1

The hang is due to:

  1. the rspec configuration in spec_helper.rb

    config.use_transactional_fixtures = true

    This initiates a postgresql BEGIN block, which initiates locking on the tables

  2. in combination with the use of pg_dump, which cannot execute due to the database locks. It hangs (forever or for a specified timeout) waiting for the locks to be removed.

So the solution is simply to set use_transactional_fixtures to false. Of course, this leaves the database with test data at the end of the spec, which must be handled another way. DatabaseCleaner can be used, but not with transaction strategy, as this will have the same problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.