ruby NoMethodError:未定义方法'belong_to' for #< RSpec::Core::ExampleGroup::Nested_4:0xa05d2a0>

3mpgtkmj  于 12个月前  发布在  Ruby
关注(0)|答案(5)|浏览(107)

所以这是一种困惑我,因为我不能完全弄清楚为什么会发生。这只发生在我的笔记本电脑(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]


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

ewm0tg9j

ewm0tg9j1#

尝试将此添加到rails_helper.rb

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

字符串

polhcujo

polhcujo2#

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

j9per5c4

j9per5c43#

我有一段时间很难用这个,然后改变了我的规格:

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

字符串
致:

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


突然就成功了

flmtquvp

flmtquvp4#

你可以这样写规格

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

字符串

o8x7eapl

o8x7eapl5#

将此添加到rails_helper.rb中的this RSpec.configure do |config| end之外

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

字符串
尝试修改,如

it { is_expected.to belong_to(:publisher).optional }

相关问题