rspec应该收到x次消息

时间:2014-07-18 08:21:39

标签: ruby rspec

我无法解决这个问题。我查看了这个doc,但是当我将.once(number).exactly(number)与Doc的示例(帐户)一起应用时,我得到了同样的错误

Failures:

  1) Profile#[] fetches the document only once
     Failure/Error: prof[:b]
       (#<Profile:0x0000000ff632e0>).fetch(no args)
           expected: 0 times
           received: 1 time
     # ./profile.rb:14:in `[]'
     # ./spec/profile_spec.rb:12:in `block (3 levels) in <top (required)>'


class Profile
  attr_accessor :fetched

  def initialize(x)
    @fetched = false
    @source = {}
  end

  def [](key)
    fetch if !@fetched
    @source[key.to_s]
  end

  private

  def fetch
    @source = {a: 3}
    @fetched = true
  end
end

规范

it 'fetches the document only once' do
  prof = Profile.new('123')
  prof.should_receive(:fetch).once
  prof[:a]
  prof[:b]
end

0 个答案:

没有答案