尝试运行rspec测试时,nil:nilClass的未定义方法'collection'

时间:2012-11-30 07:23:20

标签: ruby-on-rails ruby rspec

所以我正在学习ruby,我正在尝试为我编写的方法编写一个rspec测试。这是方法:

def self.store(body, r_time, tags, user_id, gps_x, gps_y)
  Note.create!(
    :body => body, 
    :timestamp => Time.now,
    :removal_time => r_time,
    :tags => tags, 
    :user_id => user_id,
    :gps => Gps.new(:x => gps_x, :y=> gps_y)
    )
end

这是我的rspec测试:

describe "#store" do
  it "stores a note in the DB" do
    # This doesn't work for some reason
    Note.store("test",Time.now,"test","test",0,0)
  end
end

但是,出于某种原因,我收到以下错误:

 1) Note#store stores a note in the DB
 Failure/Error: Note.store("test",Time.now,"test","test",0,0)
 NoMethodError:
   undefined method `collection' for nil:NilClass
 # /home/ubuntu/dev/cse-112/server/app/models/note.rb:71:in `store'
 # ./note_spec.rb:18:in `block (3 levels) in <top (required)>'

此外,错误所指的第71行只是我在第一个代码块中包含的方法的行号。我可以在控制台中使用相同的参数调用此方法,它工作正常,所以这对我来说有点混乱。

我已经尝试了通常的谷歌搜索错误的方法,但我找不到任何有助于我的方法,这是我迄今为止在红宝石中出现错误的经验。谁能指出我正确的方向?

0 个答案:

没有答案