Search code examples
rubyrspecchef-infradatabags

Chef::DataBag and RSpec-mocks -- undefined method `recieve'


i am trying to create a list method stub for Chef::DataBag class as follows:

$ cat spec/spec_helper.rb 
require "rspec"

RSpec.configure { |config|
  config.mock_framework = :rspec
  config.mock_with :rspec
}

$ cat spec/test_spec.rb 
require "spec_helper"
require "chef"

RSpec.describe "test" do
  describe "simple test" do
    it "mocks chef databag" do
      allow(Chef::DataBag).to recieve(:list).and_return {}
    end
  end
end

$ grep "rspec \|chef " Gemfile.lock | head -n 2
  gem "chef", "11.16.4"
  gem "rspec", "3.1.0"

when executing rspec i get the following error.

$ bundle exec rspec spec/test_spec.rb 
F

Failures:

  1) test simple test mocks chef databag
     Failure/Error: allow(Chef::DataBag).to recieve(:list).and_return {}
     NoMethodError:
       undefined method `recieve' for #<RSpec::ExampleGroups::Test::SimpleTest:0x000000049964f8>
     # ./spec/test_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.00048 seconds (files took 0.53998 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/test_spec.rb:6 # test simple test mocks chef databag

that is how i verified that Chef::DataBag has the list method:

$ bundle exec ruby -e "require 'chef'; puts Chef::DataBag.methods" | grep list
list

can you please point out what is wrong?


Solution

  • You misspelled "receive." Try this:

    allow(Chef::DataBag).to receive(:list).and_return {}