NoMethodError:未定义的方法`belongs_to'for#

问题描述 投票:3回答:4

所以这让我很困惑,因为我无法弄清楚为什么会这样。这只发生在我的笔记本电脑(Ubuntu 11.04)上,而不是其他地方。我似乎对这台计算机上的设置有些奇怪。

运行我的规范时,我不断收到以下错误:

be rake spec

给我:

NoMethodError: undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_4:0xb4eb2e4>
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-expectations-2.6.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'
/home/tom/work/ruby/litdistco-sales/spec/models/sales_item_spec.rb:5:in `block (2 levels) in <top (required)>'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `block in run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290@litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'

以下是我的spec文件中产生投诉的相关行:

describe SalesItem do
  it { should belong_to(:publisher) }
  it { should belong_to(:invoice) }

我正在运行Rails 3.1.0。这是ruby -v:

ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

任何提示/想法/想法建议非常感谢。

ruby rspec ruby-on-rails-3.1
4个回答
20
投票

尝试在rails_helper.rb中添加此内容

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

11
投票

RSpec核心没有这样的匹配器。它看起来像shoulda-matchers。只需确保它已在spec_helper中安装并加载


2
投票

你可以写这样的规格

describe SalesItem do
  describe "Associations" do
    it "belongs_to publisher" do
      assc = described_class.reflect_on_association(:publisher)
      expect(assc.macro).to eq :belongs_to
    end
  end
end

1
投票

我有一段时间这么困难,然后改变了我的规格:

describe ModelName do
  it { should belong_to(:model)}
end

至:

RSpec.describe ModelName, type: :model do
  it { should belong_to(:model)}
end

它突然起作用了

© www.soinside.com 2019 - 2024. All rights reserved.