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

时间:2018-09-24 04:20:47

标签: ruby-on-rails ruby rspec rspec-rails

我最近将我的ruby on rails项目升级到了Rails 5.0.7和ruby 2.5.1,并且我收到一个RSPEC错误undefined method预期'正在测试的不同对象。

我尝试按照建议的herespec_helper.rb文件中添加配置(尽管我进行了快速搜索,但未在任何地方找到定义的:should),herehere

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

也尝试在我的include RSpec::Matchers中加入spec_helper.rb,但随后出现更多错误:

`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中,我有以下宝石(以及其他宝石):

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做   #每次您运行规格时都会运行此代码。 结束

1 个答案:

答案 0 :(得分:1)

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

10-26

基本上,ActiveRecord模型不知道rspec的期望值,也不应该知道。如果该规范在此之前有效,那么您可能对ActiveRecord模型使用了一些修饰,这就是为什么以前适用的原因。