【问题标题】:Rspec appears to be missing the expect method ... is this correct?Rspec 似乎缺少期望方法……这是正确的吗?
【发布时间】:2012-08-06 18:59:24
【问题描述】:

我正在运行我的 rspec 测试,它正在输出以下内容:

Failure/Error: expect {
 NoMethodError:
   undefined method `[]' for nil:NilClass
 # ./spec/controllers/users_controller_spec.rb:44:in `block (4 levels) in <top (required)>'

错误引用的行是它显示“expect {”的行。我不太确定这里到底出了什么问题。

这是完整的规范:

require 'spec_helper'

describe UsersController do

  def valid_attributes
      {
      :username => "tester",
      :email => "tester@holler.com",
      :password => "testingpass"
      }
  end

  def valid_session
    {}
  end

  describe "POST create" do
    describe "with valid params" do
      it "creates a new User" do
        expect {
          post :create, {:user => valid_attributes , :format => :json}, valid_session
        }.to change(User, :count).by(1)
      end
    end
  end
end

关于这里出了什么问题的任何想法?

更新

一位评论者要求提供 spec_helper.rb 文件。我把它贴在下面……它是 rspec 生成的默认值。

# 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 'rspec/autorun'

# 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|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # 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

  # 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"
end

【问题讨论】:

  • 这对我来说很好用。我们可以看到user.rbspec_helper.rb 的任何相关部分吗?
  • 问题绝对不在于 user.rb,因为我刚刚运行了该项目,它可以完美地创建和删除用户。我正在发布一个带有 spec_helper.rb 的更新,它恰好是自动生成的版本
  • 您是否对user.rbusers_controller.rb 进行了任何更改?那将是下一个要看的地方。
  • 没有变化。如果没有报告错误,我们为什么还要检查那里?
  • 好吧,当我使用自动生成的模型、控制器和 rspec_helper 运行您的规范时,它运行良好。您对哪些文件进行了更改?

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


【解决方案1】:

它没有丢失 expect 方法 - 失败消息说它在 nil 上丢失了 []。默认情况下,RSpec 只显示导致问题的规范行,但有时问题更深层次。

在命令行上使用--backtrace 标志运行规范,以查看完整的回溯并查明真正的来源。

HTH, 大卫

【讨论】:

    猜你喜欢
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2013-09-25
    • 1970-01-01
    • 2014-10-17
    • 2011-11-11
    相关资源
    最近更新 更多