问题
One of my tests has started failing. It previously worked and going through the same actions by hand in the browser show the feature to be working.
I create the records to be tested like this:
will = FactoryGirl.create(:user, profile: "security of the internet", first_name: "will")
To investigate, I added a
binding.pry
call in my controller. The action searches Users.
If i do:
User.all
at the pry prompt, no users are shown, an empty result.
If i break into the actual test with pry, User.all shows the expected records.
What might be the cause of this?
回答1:
Usually this is because your test is tagged js: true and is using a JS capable driver but you are using transactional database testing. When using the "full browser" drivers the app and tests run. Different threads so they each have their own database connection. This means objects created in one threads transaction aren't visible in the other until committed, but when using transaction based testing nothing ever gets committed. To fix, turn off transactional testing and use truncation or deletion for that test - see database_cleaner
来源:https://stackoverflow.com/questions/37660341/records-created-with-factorygirl-not-available-in-controller