【问题标题】:Rails tests are inconsistent in their resultsRails 测试的结果不一致
【发布时间】:2020-06-27 18:13:09
【问题描述】:

我读过这篇类似的帖子。 Inconsistent Rails Test Results。 它只有一个响应,对我没有帮助。

我一直在关注这本名为 Agile Web Development with Rails 6 的新书。作者运行了这些测试,但它们对我不起作用。

这就是我所做的。

我使用new rails test 创建了一个新的rails 项目,然后运行rails generate scaffold Product title:string description:text image_url:string price:decimal。这是作者使用的命令。 然后我运行rails db:migrate RAILS_ENV=test,然后准备运行rails test

这样做之后,我在控制台中得到了这个响应。 7 runs, 5 assertions, 0 failures, 3 errors, 0 skips

这是错误。

Error:
ProductsControllerTest#test_should_get_index:
ActionView::Template::Error: Permission denied @ rb_file_s_rename - (C:/Users/Client/Desktop/Ruby on Rails/tests/tmp/cache/assets/sprockets/v4.0.0/-T/-TZMQrMJrqe1bey8hRhmqKVmncCU8rFfTfOziZyMSMQ.cache.74410620.20908.613522, C:/Users/Client/Desktop/Ruby on Rails/tests/tmp/cache/assets/sprockets/v4.0.0/-T/-TZMQrMJrqe1bey8hRhmqKVmncCU8rFfTfOziZyMSMQ.cache)
Error:
ProductsControllerTest#test_should_show_product:
ActionView::Template::Error: Permission denied @ rb_file_s_rename - (C:/Users/Client/Desktop/Ruby on Rails/tests/tmp/cache/assets/sprockets/v4.0.0/HM/HMwQEAOlQlZbn_VGqaBmumzU5MudrUMOA7mZ9lf-I3U.cache.73876560.20908.130802, C:/Users/Client/Desktop/Ruby on Rails/tests/tmp/cache/assets/sprockets/v4.0.0/HM/HMwQEAOlQlZbn_VGqaBmumzU5MudrUMOA7mZ9lf-I3U.cache)
    app/views/layouts/application.html.erb:8
    test/controllers/products_controller_test.rb:27:in `block in <class:ProductsControllerTest>'
Error:
ProductsControllerTest#test_should_destroy_product:
ActiveRecord::StatementInvalid: SQLite3::BusyException: database is locked
    app/controllers/products_controller.rb:57:in `destroy'
    test/controllers/products_controller_test.rb:43:in `block (2 levels) in <class:ProductsControllerTest>'
    test/controllers/products_controller_test.rb:42:in `block in <class:ProductsControllerTest>'

不一致的是我的结果不同,这里有一些

7 runs, 7 assertions, 0 failures, 2 errors, 0 skips

7 runs, 4 assertions, 0 failures, 4 errors, 0 skips

7 runs, 8 assertions, 0 failures, 1 errors, 0 skips

一旦我有 0 次失败和 0 次错误。我对 Ruby on Rails 完全陌生,因此不胜感激。 另外,我尝试运行bundle exec rake db:test:prepare,我在顶部链接的帖子中读到了它。这对我没有帮助。

编辑:我没有意识到这很重要,但我的项目实际上命名为测试,而不是测试。所以不会发生与 test 关键字的冲突。另外,在另一个名为“depot”的项目最初未能通过测试后,我设置了这个“测试”项目。

我的 Rails 版本:6.0.3.2

我的 Ruby 版本:2.6.6p146

以下是权限被拒绝错误的测试。 这是从这个文件中摘录的,test\controllers\products_controller_test.rb

test "should get index" do
    get products_url
    assert_response :success
end

test "should show product" do
  get product_url(@product)
  assert_response :success
end

我的宝石文件

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.6'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.1'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3', '~> 1.4'
gem 'sqlite3', git: "https://github.com/larskanis/sqlite3-ruby", branch: "add-gemspec"

# Use Puma as the app server
gem 'puma', '~> 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'webdrivers'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我的 Gemfile.lock

GIT
  remote: https://github.com/larskanis/sqlite3-ruby
  revision: 1027509079176fcc4088dcc7638101cde617cf16
  branch: add-gemspec
  specs:
    sqlite3 (1.3.13.20180326210955)

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.0.3.2)
      actionpack (= 6.0.3.2)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.0.3.2)
      actionpack (= 6.0.3.2)
      activejob (= 6.0.3.2)
      activerecord (= 6.0.3.2)
      activestorage (= 6.0.3.2)
      activesupport (= 6.0.3.2)
      mail (>= 2.7.1)
    actionmailer (6.0.3.2)
      actionpack (= 6.0.3.2)
      actionview (= 6.0.3.2)
      activejob (= 6.0.3.2)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.0.3.2)
      actionview (= 6.0.3.2)
      activesupport (= 6.0.3.2)
      rack (~> 2.0, >= 2.0.8)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.2.0)
    actiontext (6.0.3.2)
      actionpack (= 6.0.3.2)
      activerecord (= 6.0.3.2)
      activestorage (= 6.0.3.2)
      activesupport (= 6.0.3.2)
      nokogiri (>= 1.8.5)
    actionview (6.0.3.2)
      activesupport (= 6.0.3.2)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.0.3.2)
      activesupport (= 6.0.3.2)
      globalid (>= 0.3.6)
    activemodel (6.0.3.2)
      activesupport (= 6.0.3.2)
    activerecord (6.0.3.2)
      activemodel (= 6.0.3.2)
      activesupport (= 6.0.3.2)
    activestorage (6.0.3.2)
      actionpack (= 6.0.3.2)
      activejob (= 6.0.3.2)
      activerecord (= 6.0.3.2)
      marcel (~> 0.3.1)
    activesupport (6.0.3.2)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 0.7, < 2)
      minitest (~> 5.1)
      tzinfo (~> 1.1)
      zeitwerk (~> 2.2, >= 2.2.2)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    bindex (0.8.1)
    bootsnap (1.4.6)
      msgpack (~> 1.0)
    builder (3.2.4)
    byebug (11.1.3)
    capybara (3.33.0)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (~> 1.5)
      xpath (~> 3.2)
    childprocess (3.0.0)
    concurrent-ruby (1.1.6)
    crass (1.0.6)
    erubi (1.9.0)
    ffi (1.13.1-x64-mingw32)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    i18n (1.8.3)
      concurrent-ruby (~> 1.0)
    jbuilder (2.10.0)
      activesupport (>= 5.0.0)
    loofah (2.6.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (1.0.0)
    mimemagic (0.3.5)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.14.1)
    msgpack (1.3.3-x64-mingw32)
    nio4r (2.5.2)
    nokogiri (1.10.9-x64-mingw32)
      mini_portile2 (~> 2.4.0)
    public_suffix (4.0.5)
    puma (4.3.5)
      nio4r (~> 2.0)
    rack (2.2.3)
    rack-proxy (0.6.5)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (6.0.3.2)
      actioncable (= 6.0.3.2)
      actionmailbox (= 6.0.3.2)
      actionmailer (= 6.0.3.2)
      actionpack (= 6.0.3.2)
      actiontext (= 6.0.3.2)
      actionview (= 6.0.3.2)
      activejob (= 6.0.3.2)
      activemodel (= 6.0.3.2)
      activerecord (= 6.0.3.2)
      activestorage (= 6.0.3.2)
      activesupport (= 6.0.3.2)
      bundler (>= 1.3.0)
      railties (= 6.0.3.2)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (6.0.3.2)
      actionpack (= 6.0.3.2)
      activesupport (= 6.0.3.2)
      method_source
      rake (>= 0.8.7)
      thor (>= 0.20.3, < 2.0)
    rake (13.0.1)
    regexp_parser (1.7.1)
    rubyzip (2.3.0)
    sass-rails (6.0.0)
      sassc-rails (~> 2.1, >= 2.1.1)
    sassc (2.4.0-x64-mingw32)
      ffi (~> 1.9)
    sassc-rails (2.1.2)
      railties (>= 4.0.0)
      sassc (>= 2.0)
      sprockets (> 3.0)
      sprockets-rails
      tilt
    selenium-webdriver (3.142.7)
      childprocess (>= 0.5, < 4.0)
      rubyzip (>= 1.2.2)
    sprockets (4.0.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.1)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    thor (1.0.1)
    thread_safe (0.3.6)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (1.2.7)
      thread_safe (~> 0.1)
    tzinfo-data (1.2020.1)
      tzinfo (>= 1.0.0)
    web-console (4.0.3)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (4.4.1)
      nokogiri (~> 1.6)
      rubyzip (>= 1.3.0)
      selenium-webdriver (>= 3.0, < 4.0)
    webpacker (4.2.2)
      activesupport (>= 4.2)
      rack-proxy (>= 0.6.1)
      railties (>= 4.2)
    websocket-driver (0.7.2)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.3.0)

PLATFORMS
  x64-mingw32

DEPENDENCIES
  bootsnap (>= 1.4.2)
  byebug
  capybara (>= 2.15)
  jbuilder (~> 2.7)
  puma (~> 4.1)
  rails (~> 6.0.3, >= 6.0.3.1)
  sass-rails (>= 6)
  selenium-webdriver
  sqlite3!
  turbolinks (~> 5)
  tzinfo-data
  web-console (>= 3.3.0)
  webdrivers
  webpacker (~> 4.0)

RUBY VERSION
   ruby 2.6.6p146

BUNDLED WITH
   1.17.2

【问题讨论】:

  • 我认为是rails new test 而不是new rails test。什么版本的 Rails?较新的版本保留“测试”。我们可以看到测试吗?对于permission denied的错误,你在这个过程中切换用户了吗? rails tmp:clear 可能会有所帮助。
  • 我没有切换用户。通过切换用户,我假设您是指我的 Windows 操作系统上的用户帐户。我从不切换用户。
  • 啊,Windows。 Windows 对打开的文件有强制文件锁定,这可能看起来像权限错误和 SQLite 数据库锁定。在 Windows 上运行 Rails 需要更多的工作。尝试遵循Install Ruby On Rails on Windows 10 指南和Installing and troubleshooting Ruby on Rails (SQLite3 Windows 10 fix)
  • 好的,谢谢。我会研究这些链接。
  • 我在阅读中篇文章时遇到了一些问题。作者说要替换 gemfile 中的这一行,gem 'sqlite3', '~&gt; 1.4' 他的示例没有版本号,但我的有。他说用gem 'sqlite3', git: "https://github.com/larskanis/sqlite3-ruby", branch: "add-gemspec" 替换它,然后运行bundle install。之后一切似乎都很好,但是当我运行 rails server 并前往 localhost:3000 时,我在浏览器中看到了这个错误。 Puma 发现了这个错误:加载“sqlite3”Active Record 适配器时出错。缺少它所依赖的宝石? ...

标签: ruby-on-rails ruby windows testing


【解决方案1】:

较新版本的 Rails 不允许您创建名为“test”的应用程序,因为它可能与现有模块和类发生冲突。

$ rails new test
Invalid application name test. Please give a name which does not match one of the reserved rails words: application, destroy, plugin, runner, test

This was added 11 years ago 所以你的 Rails 副本可能已经过时了。升级 Rails 并尝试使用不同的名称,例如 test.rails。它适用于我™。

【讨论】:

  • 我的错,我的项目实际上被称为“测试”,我没有意识到这是一个重要的区别。我的 rails 版本是 6.0.3.2,ruby 版本是 2.6.6p146。
  • @HolyMoeller rails new tests 为我工作。它看起来像您的 tmp 目录中的权限问题。尝试清除缓存:rails tmp:clear。请edit your answer提供正确的信息,以便其他人可以提供帮助。
  • 运行rails tmp:clear 没有帮助,我的rails test 仍在产生错误。
【解决方案2】:

在 Windows 上运行“rails test”时收到相同的错误,请尝试一次只运行一个测试。为我工作。运行多个测试时,它会给出您显示的错误。这不是一个长期的解决方案,但也许它可以帮助您解决问题。

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2021-06-12
    • 2016-06-03
    • 2012-02-19
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    相关资源
    最近更新 更多