如何解决所有示例中的 Ruby on Rails 失败/错误
我正在按照本指南构建一个安静的 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`,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`,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`,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`,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
解决方法
将 shoulda-matchers 和 rspec-rails 升级到对我有用的最新版本
,我也试过那个教程。我正在使用 Ruby 2.7.2 和 Rails 6.1.3,我需要更新 gem 'rspec-rails','~> 4.0'
以解决该问题。
好的,所以问题似乎出在 ruby 和 rails 的版本上。
出于某种原因,ruby 2.7.2 和 Rails 6.0.3.4 可以解决我的问题。
当然 GemFile 需要修改
,在 gem 文件中,如果 rspec 的版本是
gem 'rspec-rails','~> 4.0'
它对我有用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。