Ruby on Rails所有示例中的Failure/Error



我遵循本指南构建一个restful json apihttps://www.digitalocean.com/community/tutorials/build-a-restful-json-api-with-rails-5-part-one#project-设置

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

C:Usersnion1todos-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 

升级should a matchers和rspec rails到最新版本对我有效

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

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

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

当然,GemFile需要更改

在gem文件中,如果rspec的版本是<4试着用最新版本或下面的行替换它。

gem'rspec rails','~>4.0’

它对我有效

我解决了,在我的情况下,函数如下所示更新gem-rspec-rails:gem 'rspec-rails', ~>4.0 gem 'shoulda-matchers', '~> 5.0'

对于轨道6.1.4.1为良好

最新更新