Skip to content

Instantly share code, notes, and snippets.

@DavidS
Created August 16, 2018 16:24
Show Gist options
  • Save DavidS/b178c2443943c35a53262eeaac94237a to your computer and use it in GitHub Desktop.
Save DavidS/b178c2443943c35a53262eeaac94237a to your computer and use it in GitHub Desktop.
describe 'something' do
let(:route) { instance_double('Net::IP::Route', 'route') }
before(:each) do
allow(Net::IP::Route).to receive(:new).with({ test: 'data' }).and_return(route)
end
it 'doesn't suck' do
expect(Net::IP).to receive(:routes).with(route)
provider.create(context, name, {input: 'foo'})
end
end
@DavidS
Copy link
Author

DavidS commented Aug 16, 2018

if you define the "unit" as the entire provider, don't mock out the puppet_munge function, it's part of the unit. Very likely that the allow(provider).to receive(:puppet_munge) doesn't actually hit.

If you're mocking everything, I'd recommend using non-production values like this, to make tracing easier:

describe 'create(context, name, should)' do
  let(:netiproute) { instance_double('Net::IP::Route', 'route') }
  before(:each) do
    allow(provider).to receive(:puppet_munge).with('should').and_return('munged')
    allow(Net::IP::Route).to receive(:new).with('munged').and_return(netiproute)
  end

  it 'does not suck' do
    expect(Net::IP).to receive(:routes).with(netiproute)
    provider.create(context, 'default', 'should')
  end
end

it still tests all that there is to the function, but the error message will be much easier to read, because there is a straightforward connection between the values and the variables.

PS: puppetlabs_spec_helper: defaults mock_withto:mocha. See https://github.com/puppetlabs/puppetlabs_spec_helper#mock_with : please set mock_with: ':rspec' in the .sync.yml, or manually set it like https://github.com/puppetlabs/pdk-templates/blob/master/moduleroot/spec/spec_helper.rb.erb#L1-L6 (yes, this needs to be the very first thing in the file

@dhollinger
Copy link

Ugh, this is getting infuriating:

With the changes you suggested. Reading the code, it looks like it should work, but I get this:

$ bundle exec rspec spec/unit/puppet/provider/network_route/network_route_spec.rb

Puppet::Provider::NetworkRoute::NetworkRoute
  #puppet_munge(should)
    should parse network_route into iproute2 keys
  #get
    processes resources
  create(context, name, should)
    does not suck (FAILED - 1)

Failures:

  1) Puppet::Provider::NetworkRoute::NetworkRoute create(context, name, should) does not suck
     Failure/Error: route = Net::IP::Route.new(should)

       #<Net::IP::Route (class)> received :new with unexpected arguments
         expected: ("munged")
              got: ("should")
        Please stub a default value first if message might be received with other args as well.
     # ./lib/puppet/provider/network_route/network_route.rb:80:in `create'
     # ./spec/unit/puppet/provider/network_route/network_route_spec.rb:79:in `block (3 levels) in <top (required)>'

Finished in 0.02734 seconds (files took 1.38 seconds to load)
3 examples, 1 failure

Failed examples:

rspec ./spec/unit/puppet/provider/network_route/network_route_spec.rb:77 # Puppet::Provider::NetworkRoute::NetworkRoute create(context, name, should) does not suck

@DavidS
Copy link
Author

DavidS commented Aug 16, 2018

I suspect the allows not to match. change them to an expect temporarily?

Also double check, maybe even pry into, that your code is doing the right thing. in long debugging sessions things can get lost

@dhollinger
Copy link

dhollinger commented Aug 16, 2018

Switching to expect results in the same error.

Using pry, I don't see anything out of the ordinary, but seeing as puppet_munge is called by the create method, is allow(provider).to receive(:puppet_munge).with('should').and_return('munged') even the correct way to mock the results of that method?

none of the allows/expects are failing, it's failing specifically on provider.create() in the test. Almost like that line partially ignores the allows

@dhollinger
Copy link

@DavidS
Copy link
Author

DavidS commented Aug 17, 2018

To wrap up this thread, after some online debugging we found a missing stub, causing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment