Chef 13 Upgrade: Rubocop Changes for Testing render_file with ChefSpec and a with_content Block

Featured image for sharing metadata for article
As part of an upgrade from Chef 12 to Chef 13, this is one of the posts in which I've been detailing the issues I've encountered, and how I've resolved them .

When testing that Chef's templates are being rendered correctly, the easiest way to do this is via render_file(...).with_content(&block).

However, when running the below code against Chef 13's Rubocop, this gives the error Parenthesize the param render_file:

expect(chef_run).to render_file('/chef/.ssh/authorized_keys')
  .with_content do |content|
    expect(content).to match(%r{^ssh-rsa this is long key$})
    expect(content).to match(%r{^ssh-another key$})
    expect(content).to match(%r{^wibble deploy$})
  end

The fix is to have the whole render_file method call wrapped in parentheses, such as:

-expect(chef_run).to render_file('/chef/.ssh/authorized_keys')
-  .with_content { |content|
+expect(chef_run).to(render_file('/chef/.ssh/authorized_keys')
+  .with_content do |content|
     expect(content).to match(%r{^ssh-rsa this is long key$})
     expect(content).to match(%r{^ssh-another key$})
     expect(content).to match(%r{^wibble deploy$})
-  }
+  end)

Additionally, I've converted the multi-line block to a do / end block, as that was another complaint of Rubocop.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #chef-13-upgrade #chef-13-upgrade-chefspec #chef #chefspec.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.