Skip to content

[Feature Idea] Mock Commands #43

Description

@albertalef

Problem: Testing RubyShell scripts requires actually executing system commands, making tests slow, non-deterministic, and dependent on system state and installed tools.

Solution: Provide RubyShell.mock to define fake command outputs for testing, enabling fast and isolated unit tests.

require "rubyshell/testing"

RSpec.describe "Deploy script" do
  before do
    RubyShell.mock(:git, "status").to_return { "nothing to commit" }
    RubyShell.mock(:git, "pull").to_return { "Already up to date." }
    RubyShell.mock(:ls).to_return { "file1.txt\nfile2.txt" }
  end

  it "checks git status" do
    result = sh { git("status") }
    expect(result).to include("nothing to commit")
  end
end

# Mock to raise errors
RubyShell.mock(:rm).to_raise(RubyShell::CommandError, command: "rm", stderr: "Permission denied")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions