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

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 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.

Categories:

Tags:

Other posts you may be interested in:

Interactions with this post

This post has had 0 WebMention interactions, as of Sun, 24 Mar 2019 13:54:27 +0000.

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