Search code examples
rubytestingenvironment-variablestddrspec2

Testing RUBY_PLATFORM with RSpec 2


Is there a way to change the RUBY_PLATFORM constant so that I can test the following method?

def determine_os
  case RUBY_PLATFORM
    when /darwin/ then :mac
    when /linux/ then :linux
    else raise InvalidOSError
  end
end

Solution

  • RUBY_PLATFORM shouldn't be used to determine operating system. Use the appropriate item within the hash RbConfig instead.

    Also, if you want to make it more easily tested, you could do

    def determine_os(os_string)
      case os_string
        when /darwin/ then :mac
        when /linux/ then :linux
        else raise InvalidOSError
      end
    end
    

    and you could do determine_os("darwin") for your test.