Marthinus Engelbrecht
Marthinus Engelbrecht

Reputation: 451

How do you unit test a custom chef resource in isolation with ChefSpec?

So I'm trying to create a library of reusable custom resources. This video explains how to do that but it doesn't address testing the resource in isolation.

I know you can test a resource through a recipe and a converge, but I don't want to write a new recipe just to test my resource.

I was hoping there is some way to execute a custom resource with properties without needing to do something like below:

let(:chef_run) do
    runner = ChefSpec::ServerRunner.new
    runner.converge(described_recipe)
end 

If you could just say something like say

filename = 'myFile'    

it 'my resource should write to file' do
  execute_resouce(:some_resouce).with(filename)
  expect(::File).to receive(:write).with(filename)
end

it would be really cool.

Upvotes: 0

Views: 559

Answers (1)

coderanger
coderanger

Reputation: 54267

This is, unfortunately, rather difficult. I have a solution that works for me, but it isn't fully cleaned up for public use yet. You can see the in-progress clean up at https://github.com/poise/poise-spec and maybe crib some ideas from my style (eg. https://github.com/poise/poise-service/blob/master/test/spec/resources/poise_service_user_spec.rb). Overall the easiest way to go for now is just make a test fixture cookbook under test/cookbook/ or similar and put a bunch of tiny recipes in it that you can tell ChefSpec to run.

Upvotes: 1

Related Questions