Back to all reviewers

Use strict test doubles

chef/chef
Based on 4 comments
Ruby

Always use strict test doubles (instance_double, class_double) instead of basic doubles or OpenStruct in tests. Strict doubles provide compile-time checks that prevent tests from passing when the mocked interface changes, improving test maintainability and catching integration issues early.

Testing Ruby

Reviewer Prompt

Always use strict test doubles (instance_double, class_double) instead of basic doubles or OpenStruct in tests. Strict doubles provide compile-time checks that prevent tests from passing when the mocked interface changes, improving test maintainability and catching integration issues early.

Example - Instead of:

auth_stub = double("vault auth", aws_iam: nil)
dummy = OpenStruct.new(stdout: output, exitstatus: 0)

Use:

auth_stub = instance_double("VaultAuth", aws_iam: nil)
shell_out = instance_double(Mixlib::ShellOut,
  stdout: output,
  exitstatus: 0,
  error?: false
)

This approach:

  • Ensures mocks accurately reflect the real interfaces
  • Catches interface changes during test execution
  • Makes dependencies explicit in test code
  • Prevents tests from silently passing with invalid assumptions
4
Comments Analyzed
Ruby
Primary Language
Testing
Category

Source Discussions