Rspec模型关联

时间:2012-08-09 14:09:06

标签: rspec associations

我无法通过rspec测试

规格/模型/ ticket_spec.rb

describe Ticket do
  describe "should have associations" do
    it { should belong_to(:location) }

bash

  1) Ticket should have associations 
     Failure/Error: it { should belong_to(:location) }
     NoMethodError:
       undefined method `relations' for #<Class:0x000000076a34a0>
     # ./spec/models/ticket_spec.rb:5:in `block (3 levels) in <top (required)>'

模型/ ticket.rb

class Ticket < ActiveRecord::Base 
  ...
  belongs_to :location

的Gemfile

group :test do
  ...
  gem "shoulda"
  gem "shoulda-matchers"
end

1 个答案:

答案 0 :(得分:1)

如果您正在使用主题功能,则应定义该主题:

before { @ticket = ... }
subject { @ticket }

然后您可以将其用作:

it { should belong_to(:location) }