Rspec 对象的未定义方法“期望”

Rspec undefined method `expects' for Object

我最近将我在 rails 项目上的 ruby 升级到了 rails 5.0.7 和 ruby 2.5.1,我得到了一个 RSPEC错误 undefined method预期我正在测试的`不同对象。

我尝试按照建议在 spec_helper.rb 文件中添加配置 here (although I did a quick search and didn't find :should defined anywhere), here and here:

config.expect_with :rspec do |expectations|
  expectations.syntax = [:expect, :expects]
end

也尝试在我的 spec_helper.rb 中包含 include RSpec::Matchers,但后来我得到更多错误:

`only the `receive`, `have_received` and `receive_messages` matchers are supported with `expect(...).to`, but you have provided: #<RSpec::Matchers::BuiltIn::Eq:0x00007f962b3db058>`

我如何使用 expect 的示例:

describe "process" do
before :each do
  @item = Item.new
end

it "parses the uploaded file and extract the correct report data" do
  item_a_data = {"name" => "one"}
  item_b_data = {"name" => "two"}
  file_contents = {"items" => [item_a_data, item_b_data]}
  @item.data_file = double("file", :read => file_contents.to_json)
  @item.name = item_a_data["name"]
  @item.expects(:interpret_json_file).with(@item.data_file).returns(file_contents)
  @item.expects(:save).returns(true)

  expect(@item.process_data_file).to be_truthy
  expect(@item.data).to eq item_a_data.to_json
end

请注意,当我调用 @item.expects

时出现错误

在我的 Gemfile 中,我有以下 gem(以及其他):

group :development, :test do
  gem 'byebug'
  gem 'sqlite3'
  gem 'rspec-rails'
  gem 'rb-fsevent', '~> 0.9.1'
  gem 'guard-rspec', '4.7.3'
  gem 'guard-spork', '2.1.0'
  gem 'spork', '0.9.2'
  gem 'jasmine-rails'
  gem 'teaspoon-jasmine'
end

group :test do
  gem 'capybara'
  gem 'cucumber-rails', :require => false
  gem 'database_cleaner'
  gem 'factory_girl_rails', '4.1.0'
  gem 'launchy'
  gem 'poltergeist'
  gem 'timecop'
  gem 'webmock'
  gem 'simplecov', '~> 0.16.0'
  gem 'rails-controller-testing'
end

spec_helper.rb:

require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'

Spork.prefork do
 require 'simplecov'
  SimpleCov.start 'rails'
  # This file is copied to spec/ when you run 'rails generate rspec:install'
  ENV["RAILS_ENV"] ||= 'test'
  require File.expand_path("../../config/environment", __FILE__)
  require 'rspec/rails'
  require 'devise'
  require './spec/controllers/controller_helpers.rb'

  # Requires supporting ruby files with custom matchers and macros, etc,
  # in spec/support/ and its subdirectories.
  Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

  RSpec.configure do |config|
    config.include Devise::Test::ControllerHelpers, type: :controller
    config.include ControllerHelpers, type: :controller

    config.include Capybara::DSL

    # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
    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

    # If true, the base class of anonymous controllers will be inferred
    # automatically. This will be the default behavior in future versions of
    # rspec-rails.
    config.infer_base_class_for_anonymous_controllers = false

    config.infer_spec_type_from_file_location!

    # Run specs in random order to surface order dependencies. If you find an
    # order dependency and want to debug it, you can fix the order by providing
    # the seed, which is printed after each run.
    #     --seed 1234
    config.order = "random"
    config.include FactoryGirl::Syntax::Methods

    # config.expect_with :rspec do |expectations|
    #   expectations.syntax = :expect
    # end
 end
end

Spork.each_run 做 # 每次您 运行 您的规格时,此代码将是 运行。 结束

解决方案是将其重写为以下语法:

expect(@item).to receive(:interpret_json_file).with(@item.data_file) { file_contents } 
expect(@item).to receive(:save) { true }

基本上 ActiveRecord 模型不知道 rspec 预期,也不应该知道。如果该规范之前有效,那么您可能会为 ActiveRecord 模型使用一些装饰,这就是它之前有效的原因。