После перехода на postgresql из sqlite некоторые тесты Rspec перестали проходить

#ruby-on-rails #postgresql #sqlite #rspec

#ruby-on-rails #postgresql #sqlite #rspec

Вопрос:

Я создал приложение Rails с базой данных sqlite. Через некоторое время я решил перейти на postgresql и в среде разработки. Я использую postgresql в рабочей среде (Heroku).

У меня 59 тестов Rspec, и все они проходят при использовании sqlite. Но после перехода на postgresql некоторые из них начинают давать сбой. Я заметил, что в некоторых примерах FactoryGirl не создавала объекты, поэтому приложение выдает ошибку при переходе, например, по /words/1 URL, что оно не смогло найти Word с таким идентификатором.

Единственным способом, которым я смог заставить тесты проходить снова, было изменение конфигурации database_cleaner.

От:

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

Для:

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

Другим сложным способом прохождения всех тестов было создание их всех js: true .

У вас есть какие-либо идеи, как исправить эту проблему? Какое здесь правильное решение?

Ошибки после запуска тестов:

 Failures:

  1) User adds comment when signed in and try to add empty comment
     Failure/Error: fill_in 'Treść komentarza:', with: ''

     Capybara::ElementNotFound:
       Unable to find field "Treść komentarza:"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/actions.rb:59:in `fill_in'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_adds_comment_spec.rb:25:in `block (2 levels) in <top (required)>'

  2) User adds comment when signed out
     Failure/Error: expect(page).to have_content 'Zaloguj się, aby dodać komentarz'
       expected to find text "Zaloguj się, aby dodać komentarz" in "ActiveRecord::RecordNotFound at /words/1 ======================================== > Couldn't find Word with 'id'=1 app/views/words/show.html.haml, line 1 -------------------------------------- ``` ruby > 1 - title "#{word.pl} po angielsku to #{word.en}" 2 .row 3 .col-md-12 4 %h1= word.en 5 .col-md-4 6 %p ``` App backtrace ------------- - app/views/words/show.html.haml:1:in `_app_views_words_show_html_haml__1962247352459501123_87235100' - spec/features/user_adds_comment_spec.rb:33:in `block (2 levels) in ' Full backtrace -------------- - activerecord (4.2.6) lib/active_record/relation/finder_methods.rb:324:in `raise_record_not_found_exception!' - activerecord (4.2.6) lib/active_record/relation/finder_methods.rb:444:in `find_one' - activerecord (4.2.6) lib/active_record/relation/finder_methods.rb:423:in `find_with_ids' - activerecord (4.2.6) lib/active_record/relation/finder_methods.rb:71:in `find' - decent_exposure (2.3.3) lib/decent_exposure/active_record_strategy.rb:60:in `singular_resource' - decent_exposure (2.3.3) lib/decent_exposure/active_record_strategy.rb:70:in `resource' - decent_exposure (2.3.3) lib/decent_exposure/strategies/assign_from_method.rb:14:in `resource' - decent_exposure (2.3.3) lib/decent_exposure/exposure.rb:14:in `call' - decent_exposure (2.3.3) lib/decent_exposure/expose.rb:53:in `block in define_exposure_methods' - actionpack (4.2.6) lib/abstract_controller/helpers.rb:67:in `word' - app/views/words/show.html.haml:1:in `_app_views_words_show_html_haml__1962247352459501123_87235100' - actionview (4.2.6) lib/action_view/template.rb:145:in `block in render' - activesupport (4.2.6) lib/active_support/notifications.rb:166:in `instrument' - actionview (4.2.6) lib/action_view/template.rb:333:in `instrument' - actionview (4.2.6) lib/action_view/template.rb:143:in `render' - actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template' - actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument' - activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument' - activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' - activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument' - actionview (4.2.6) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument' - actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template' - actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout' - actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:52:in `render_template' - actionview (4.2.6) lib/action_view/renderer/template_renderer.rb:14:in `render' - actionview (4.2.6) lib/action_view/renderer/renderer.rb:46:in `render_template' - actionview (4.2.6) lib/action_view/renderer/renderer.rb:27:in `render' - actionview (4.2.6) lib/action_view/rendering.rb:100:in `_render_template' - actionpack (4.2.6) lib/action_controller/metal/streaming.rb:217:in `_render_template' - actionview (4.2.6) lib/action_view/rendering.rb:83:in `render_to_body' - actionpack (4.2.6) lib/action_controller/metal/rendering.rb:32:in `render_to_body' - actionpack (4.2.6) lib/action_controller/metal/renderers.rb:37:in `render_to_body' - actionpack (4.2.6) lib/abstract_controller/rendering.rb:25:in `render' - actionpack (4.2.6) lib/action_controller/metal/rendering.rb:16:in `render' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render' - activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms' - /home/hubert/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime' - activesupport (4.2.6) lib/active_support/core_ext/benchmark.rb:12:in `ms' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime' - activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:43:in `render' - remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart' - actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:10:in `default_render' - actionpack (4.2.6) lib/action_controller/metal/implicit_render.rb:4:in `send_action' - actionpack (4.2.6) lib/abstract_controller/base.rb:198:in `process_action' - actionpack (4.2.6) lib/action_controller/metal/rendering.rb:10:in `process_action' - actionpack (4.2.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action' - activesupport (4.2.6) lib/active_support/callbacks.rb:117:in `call' - activesupport (4.2.6) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile' - activesupport (4.2.6) lib/active_support/callbacks.rb:505:in `call' - activesupport (4.2.6) lib/active_support/callbacks.rb:92:in `__run_callbacks__' - activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks' - activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' - actionpack (4.2.6) lib/abstract_controller/callbacks.rb:19:in `process_action' - actionpack (4.2.6) lib/action_controller/metal/rescue.rb:29:in `process_action' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action' - activesupport (4.2.6) lib/active_support/notifications.rb:164:in `block in instrument' - activesupport (4.2.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument' - activesupport (4.2.6) lib/active_support/notifications.rb:164:in `instrument' - actionpack (4.2.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action' - actionpack (4.2.6) lib/action_controller/metal/params_wrapper.rb:250:in `process_action' - activerecord (4.2.6) lib/active_record/railties/controller_runtime.rb:18:in `process_action' - actionpack (4.2.6) lib/abstract_controller/base.rb:137:in `process' - actionview (4.2.6) lib/action_view/rendering.rb:30:in `process' - actionpack (4.2.6) lib/action_controller/metal.rb:196:in `dispatch' - actionpack (4.2.6) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' - actionpack (4.2.6) lib/action_controller/metal.rb:237:in `block in action' - actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:74:in `dispatch' - actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:43:in `serve' - actionpack (4.2.6) lib/action_dispatch/journey/router.rb:43:in `block in serve' - actionpack (4.2.6) lib/action_dispatch/journey/router.rb:30:in `serve' - actionpack (4.2.6) lib/action_dispatch/routing/route_set.rb:817:in `call' - rack-pjax (0.8.0) lib/rack/pjax.rb:12:in `call' - warden (1.2.6) lib/warden/manager.rb:35:in `block in call' - warden (1.2.6) lib/warden/manager.rb:34:in `call' - rack (1.6.4) lib/rack/etag.rb:24:in `call' - rack (1.6.4) lib/rack/conditionalget.rb:25:in `call' - rack (1.6.4) lib/rack/head.rb:13:in `call' - remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/params_parser.rb:27:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/flash.rb:260:in `call' - rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context' - rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/cookies.rb:560:in `call' - activerecord (4.2.6) lib/active_record/query_cache.rb:36:in `call' - activerecord (4.2.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' - activesupport (4.2.6) lib/active_support/callbacks.rb:88:in `__run_callbacks__' - activesupport (4.2.6) lib/active_support/callbacks.rb:778:in `_run_call_callbacks' - activesupport (4.2.6) lib/active_support/callbacks.rb:81:in `run_callbacks' - actionpack (4.2.6) lib/action_dispatch/middleware/callbacks.rb:27:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/remote_ip.rb:78:in `call' - better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call' - better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call' - better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' - web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call' - web-console (2.3.0) lib/web_console/middleware.rb:18:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' - railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app' - railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call' - activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged' - activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged' - activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged' - railties (4.2.6) lib/rails/rack/logger.rb:20:in `call' - request_store (1.3.1) lib/request_store/middleware.rb:9:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call' - rack (1.6.4) lib/rack/methodoverride.rb:22:in `call' - rack (1.6.4) lib/rack/runtime.rb:18:in `call' - activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call' - rack (1.6.4) lib/rack/lock.rb:17:in `call' - actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call' - rack (1.6.4) lib/rack/sendfile.rb:113:in `call' - railties (4.2.6) lib/rails/engine.rb:518:in `call' - railties (4.2.6) lib/rails/application.rb:165:in `call' - rack (1.6.4) lib/rack/urlmap.rb:66:in `block in call' - rack (1.6.4) lib/rack/urlmap.rb:50:in `call' - rack-test (0.6.3) lib/rack/mock_session.rb:30:in `request' - rack-test (0.6.3) lib/rack/test.rb:244:in `process_request' - rack-test (0.6.3) lib/rack/test.rb:58:in `get' - /home/hubert/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/forwardable.rb:184:in `get' - capybara (2.7.1) lib/capybara/rack_test/browser.rb:61:in `process' - capybara (2.7.1) lib/capybara/rack_test/browser.rb:36:in `process_and_follow_redirects' - capybara (2.7.1) lib/capybara/rack_test/browser.rb:22:in `visit' - capybara (2.7.1) lib/capybara/rack_test/driver.rb:43:in `visit' - capybara (2.7.1) lib/capybara/session.rb:233:in `visit' - capybara (2.7.1) lib/capybara/dsl.rb:52:in `block (2 levels) in ' - rspec-rails (3.5.0) lib/rspec/rails/example/feature_example_group.rb:29:in `visit' - spec/features/user_adds_comment_spec.rb:33:in `block (2 levels) in ' - rspec-core (3.5.0) lib/rspec/core/example.rb:252:in `block in run' - rspec-core (3.5.0) lib/rspec/core/example.rb:494:in `block in with_around_and_singleton_context_hooks' - rspec-core (3.5.0) lib/rspec/core/example.rb:451:in `block in with_around_example_hooks' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:471:in `block in run' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:611:in `block in run_around_example_hooks_for' - rspec-core (3.5.0) lib/rspec/core/example.rb:336:in `call' - rspec-rails (3.5.0) lib/rspec/rails/adapters.rb:127:in `block (2 levels) in ' - rspec-core (3.5.0) lib/rspec/core/example.rb:441:in `instance_exec' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:382:in `execute_with' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:613:in `block (2 levels) in run_around_example_hooks_for' - rspec-core (3.5.0) lib/rspec/core/example.rb:336:in `call' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:614:in `run_around_example_hooks_for' - rspec-core (3.5.0) lib/rspec/core/hooks.rb:471:in `run' - rspec-core (3.5.0) lib/rspec/core/example.rb:451:in `with_around_example_hooks' - rspec-core (3.5.0) lib/rspec/core/example.rb:494:in `with_around_and_singleton_context_hooks' - rspec-core (3.5.0) lib/rspec/core/example.rb:249:in `run' - rspec-core (3.5.0) lib/rspec/core/example_group.rb:613:in `block in run_examples' - rspec-core (3.5.0) lib/rspec/core/example_group.rb:609:in `run_examples' - rspec-core (3.5.0) lib/rspec/core/example_group.rb:575:in `run' - rspec-core (3.5.0) lib/rspec/core/runner.rb:113:in `block (3 levels) in run_specs' - rspec-core (3.5.0) lib/rspec/core/runner.rb:113:in `block (2 levels) in run_specs' - rspec-core (3.5.0) lib/rspec/core/configuration.rb:1837:in `with_suite_hooks' - rspec-core (3.5.0) lib/rspec/core/runner.rb:112:in `block in run_specs' - rspec-core (3.5.0) lib/rspec/core/reporter.rb:77:in `report' - rspec-core (3.5.0) lib/rspec/core/runner.rb:111:in `run_specs' - rspec-core (3.5.0) lib/rspec/core/runner.rb:87:in `run' - rspec-core (3.5.0) lib/rspec/core/runner.rb:71:in `run' - rspec-core (3.5.0) lib/rspec/core/runner.rb:45:in `invoke' - rspec-core (3.5.0) exe/rspec:4:in `' - () home/hubert/.rvm/gems/ruby-2.3.0/bin/rspec:23:in `' - () home/hubert/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `'"
     # ./spec/features/user_adds_comment_spec.rb:35:in `block (2 levels) in <top (required)>'

  3) User create word with missing en value
     Failure/Error: find(:css, ".check_boxes[value='1']").set(true)

     Capybara::ElementNotFound:
       Unable to find css ".check_boxes[value='1']"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_creates_word_spec.rb:26:in `block (2 levels) in <top (required)>'

  4) User create word with missing pl value
     Failure/Error: find(:css, ".check_boxes[value='1']").set(true)

     Capybara::ElementNotFound:
       Unable to find css ".check_boxes[value='1']"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_creates_word_spec.rb:33:in `block (2 levels) in <top (required)>'

  5) User create word try add the same word twice
     Failure/Error: find(:css, ".check_boxes[value='1']").set(true)

     Capybara::ElementNotFound:
       Unable to find css ".check_boxes[value='1']"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_creates_word_spec.rb:48:in `block (2 levels) in <top (required)>'

  6) User create word strip word if add unnecessary spaces
     Failure/Error: find(:css, ".check_boxes[value='1']").set(true)

     Capybara::ElementNotFound:
       Unable to find css ".check_boxes[value='1']"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_creates_word_spec.rb:64:in `block (2 levels) in <top (required)>'

  7) User does not see import option when signed in and he is admin
     Failure/Error: User.find(1).update(admin: true)

     ActiveRecord::RecordNotFound:
       Couldn't find User with 'id'=1
     # ./spec/features/user_does_not_see_import_option_spec.rb:19:in `block (2 levels) in <top (required)>'

  8) User can only edit his words  that belongs to him
     Failure/Error: find(:css, ".check_boxes[value='1']").set(true)

     Capybara::ElementNotFound:
       Unable to find css ".check_boxes[value='1']"
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # /home/hubert/.rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/user_edits_word_spec.rb:17:in `block (2 levels) in <top (required)>'
  

Комментарии:

1. Пожалуйста, опубликуйте некоторые сбои

2. Я добавил @CdotStrifeVII. Вы даже можете клонировать этот репозиторий и запускать тесты, потому что это просто мое учебное приложение. Код здесь: github.com/hubertjakubiak/angipl

Ответ №1:

Мое решение для этого — приведенный ниже код. Я использую только :truncation для функциональных тестов. Я надеюсь, что это кому-то поможет. Вероятно, ответ на то, почему это так работает, находится здесь: https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example

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

config.before(:each, type: :feature) do
  DatabaseCleaner.strategy = :truncation
end