时会出现未定义的方法

时会出现未定义的方法

本文介绍了为什么在运行 Rspec 时会出现未定义的方法“有"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到 Rails 4,除了我的 Rspec 测试外,一切正常.

I recently upgraded to Rails 4 and everything works fine except for my Rspec tests.

require 'spec_helper'

describe Invoice do

  before :each do
    @user = FactoryGirl.create(:activated_user)
    person = FactoryGirl.create(:person, :user => @user, :company => nil)
    @project = FactoryGirl.create(:project, :user => @user, :person_ids => [person.id], :invoice_recipient_id => person.id)
  end

  it "has a valid factory" do
    expect(FactoryGirl.build(:invoice, :project => @project, :user => @user)).to be_valid
  end

  it "is invalid without a number" do
    expect(FactoryGirl.build(:invoice, :project => @project, :user => @user, :number => nil)).to have(1).errors_on(:number)
  end

end

运行这些测试时,我收到此错误:

When running these tests I get this error:

Failure/Error: expect(FactoryGirl.build(:invoice, :project => @project, :user => @user, :number => nil)).to have(1).errors_on(:number)
NoMethodError:
undefined method `have' for #<RSpec::ExampleGroups::Invoice_2:0x009ge29360d910>
# ./spec/models/invoice_spec.rb:16:in `block (2 levels) in <top (required)>'

谁能告诉我我在这里遗漏了什么?

Can anybody tell me what I am missing here?

我已经用谷歌搜索过了,但没有任何结果.have 方法在 Rspec 中实际上是相当标准的,我不明白为什么它不应该工作.

I googled it already but nothing came up. The have method is actually fairly standard in Rspec and I can't see why it shouldn't work.

感谢您的指点.

推荐答案

have 匹配器系列在 RSpec 2.99 中已弃用,并已移至单独的 rspec-collection_matchers gem 从 RSpec 3.0 开始.这在 http 中有讨论://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released,其中还提供了迁移到 3.0 的建议方法.具体来说,它建议安装/使用 RSpec 2.99,以便查看与 3.0 中删除/移动的项目相关联的弃用消息.

The have family of matchers was deprecated in RSpec 2.99 and has been moved to a separate rspec-collection_matchers gem as of RSpec 3.0. This is discussed in http://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released, which also gives the suggested approach to migrating to 3.0. Specifically, it recommends installing/using RSpec 2.99 in order to see the deprecation messages associated with items that were removed/moved in 3.0.

这篇关于为什么在运行 Rspec 时会出现未定义的方法“有"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:40