Ruby 关于 Rails Failure/Error 关于所有示例

Ruby on Rails Failure/Error on all examples

我正在按照本指南构建 restful json api https://www.digitalocean.com/community/tutorials/build-a-restful-json-api-with-rails-5-part-one#project-setup

我已经跟踪了每一个步骤,直到模型区域(有 5 个区域,先决条件,项目设置,模型,控制器,结论)开始测试...... 然而,当我尝试 运行 任何测试时,我所有的示例(其中 5 个)都失败了。

C:\Users\nion1\todos-api>bundle exec rspec
FFFFF
Failures:

1) Item
 Failure/Error: example.run
   `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
 # ./spec/rails_helper.rb:53:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:52:in `block (2 levels) in <top (required)>'

2) Item
 Failure/Error: example.run
   `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
 # ./spec/rails_helper.rb:53:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:52:in `block (2 levels) in <top (required)>'

3) Todo
 Failure/Error: example.run
   `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
 # ./spec/rails_helper.rb:53:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:52:in `block (2 levels) in <top (required)>'

4) Todo
 Failure/Error: example.run
   `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
 # ./spec/rails_helper.rb:53:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:52:in `block (2 levels) in <top (required)>'

5) Todo
 Failure/Error: example.run
   `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
 # ./spec/rails_helper.rb:53:in `block (3 levels) in <top (required)>'
 # ./spec/rails_helper.rb:52:in `block (2 levels) in <top (required)>'

 Finished in 0.10099 seconds (files took 1.52 seconds to load)
 5 examples, 5 failures

 Failed examples:

 rspec ./spec/models/item_spec.rb:8 # Item
 rspec ./spec/models/item_spec.rb:11 # Item
 rspec ./spec/models/todo_spec.rb:8 # Todo
 rspec ./spec/models/todo_spec.rb:11 # Todo
 rspec ./spec/models/todo_spec.rb:12 # Todo

我真的很想了解修复此错误的方法,但我就是想不出来.. 有任何想法吗 ?任何事情都会很有帮助 我的文件:

rails_helper.rb:

require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.include FactoryBot::Syntax::Methods
  # start by truncating all the tables but then use the faster transaction strategy the rest of the time.
  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner.strategy = :transaction
  end

  # start the transaction strategy as examples are run
  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
end

spec/models/todo_spec.rb:

require 'rails_helper'

# Test suite for the Todo model
RSpec.describe Todo, type: :model do
  # Association test
  # ensure Todo model has a 1:m relationship with the Item model
  it { should have_many(:items).dependent(:destroy) }
  # Validation tests
  # ensure columns title and created_by are present before saving
  it { should validate_presence_of(:title) }
  it { should validate_presence_of(:created_by) }
end 

item_spec.rb:

require 'rails_helper'

# Test suite for the Item model
RSpec.describe Item, type: :model do
  # Association test
  # ensure an item record belongs to a single todo record
  it { should belong_to(:todo) }
  # Validation test
  # ensure column name is present before saving
  it { should validate_presence_of(:name) }
end 

好的,所以问题似乎是 ruby 和 rails 的版本。

出于某种原因 ruby 2.7.2 和 Rails 6.0.3.4 解决了我的问题。

当然需要修改GemFile

将 shoulda-matchers 和 rspec-rails 升级到最新版本对我有用

我也试过那个教程。我正在使用 Ruby 2.7.2Rails 6.1.3,我需要更新 gem 'rspec-rails', '~> 4.0' 以解决该问题。

在 gem 文件中,如果 rspec 的版本小于 4,请尝试将其替换为最新版本或以下行。

gem 'rspec-rails', '~> 4.0'

对我有用

我解决了,在我的例子中,函数就像这里说的那样更新 gem rspec-rails 如下: gem 'rspec-rails', ~>4.0 gem 'shoulda-matchers', '~> 5.0'

对于rails 6.1.4.1 没问题