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")
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.mockto define fake command outputs for testing, enabling fast and isolated unit tests.