Skip to content

Complete refactoring of call_count patterns in compiler_spec.rb #855

@justin808

Description

@justin808

Summary

PR #854 refactored some tests in spec/shakapacker/compiler_spec.rb to use explicit have_received/not_to have_received assertions instead of indirect call_count counter patterns. However, several tests still use the anti-pattern and should be refactored for consistency.

Remaining tests to refactor

The following tests still use call_count patterns:

  1. Line ~112: "handles hook with both stdout and stderr"
  2. Line ~168: "handles hook commands with spaces in paths using Shellwords"
  3. Line ~194: "supports hooks with additional arguments" (if exists)
  4. Line ~241: "passes configured environment variables to precompile_hook" (if exists)
  5. Line ~274: "merges hook environment variables with Rails.application.config_for" (if exists)

Why this matters

  • Consistency: All tests should follow the same pattern
  • Self-documenting: Explicit assertions make test intent clearer
  • Robustness: Tests won't break if unrelated implementation details change
  • Better failure messages: "expected not to receive X" vs "expected 1, got 2"

Additional considerations

Mock setup patterns

The codebase now has two patterns:

  • Tests that need to differentiate calls: Use conditional blocks
  • Tests where hooks aren't called: Use simple and_return

This is reasonable but worth documenting.

Skip test assertion

The "skips precompile_hook when SHAKAPACKER_SKIP_PRECOMPILE_HOOK=true" test uses:

expect(Open3).not_to have_received(:capture3).with(anything, hook_command, anything)
expect(Open3).to have_received(:capture3).once

This could theoretically pass if the hook was called with different arguments. Consider making the mock setup more explicit.

Related

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions