问题
I run my rspec and most of the test were failed. I got the same error for them, which is:
Failure/Error: Unable to find matching line from backtrace
ActiveRecord::StatementInvalid:
PG::ConnectionBad: PQsocket() can't get socket descriptor: BEGIN
I found a question that is similar to my problem, but there is no answer yet and I also tried the solution from this link, but it didn't make any differences for me. I opened my test console and run some simplest queries and it worked.
回答1:
There may be a problem with pg: '0.18.1'
. I tried reverting to 0.18.0
, and I believe the problem has gone away.
I'll open up an issue on the repo after testing a bit more locally. I added this issue on the pg repo.
EDIT: I was wrong. What did in fact help though, was installing the spring-commands-rspec gem. I think what it ended up doing for me was caching it in a state where it worked. Although it could also just as easily cache it in a state where it didn't.
回答2:
I don't know if the issues are linked- but I found that by using the local unix socket file instead of localhost that the issue seemed to go away for the linked question; perhaps it might help you too?
"Rake spec" failing most tests, but "rails s" working fine on Diaspora source
回答3:
I was getting the same error, and some other errors like PG::UnableToSend: socket not open
. Seemed to error at random times. I traced it down and it was actually due to a model having an improper belongs_to
association. This might not apply to you but hopefully it helps someone.
I had:
belongs_to :user, class_name: User
When it should have been:
belongs_to :user, class_name: 'User'
Somehow that one line was breaking all of my PG sockets connections.
回答4:
May not have any bearing on rspec, but throwing this out there just in case. I fought this for weeks and for me it was the way passenger spawns new processes. I just had to add this
# Rails -- prepared statements incompatible with faster spawn methods.
PassengerSpawnMethod conservative
to the \etc\apache2\mods-available\passenger.conf
https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/
回答5:
I had installed 'pg', '0.20.0'
, by downgrading to 0.19.0 it worked for me.
来源:https://stackoverflow.com/questions/23626372/rspec-pgconnectionbad-pqsocket-cant-get-socket-descriptor