Module: Yast::RSpec::Helpers

Defined in:
src/ruby/yast/rspec/helpers.rb

Class Method Summary collapse

Class Method Details

.define_yast_module(name, methods: [:fake_method], force: false, &block) ⇒ Object

Note:

This needs to be called after starting code coverage with SimpleCov.start so the coverage of the imported modules is correctly counted.

Note:

You can force using the defined stubs although the modules are present in the system, this might be useful to actually test the stubs. Just define the YAST_FORCE_MODULE_STUBS environment variable.

Helper for defining YaST modules which might not be present in the system when running the tests.

When the requested module is present in the system then it is imported as usually. If it is missing a substitute definition is created.

In the substitute definition it is enough to define only the methods used in the tests, you do not need to mirror the complete API.

Yast::RSpec::Helpers.define_yast_module("Language")

Yast::RSpec::Helpers.define_yast_module("Language", methods: [:language])

Yast::RSpec::Helpers.define_yast_module("Language") do def language "en_US" end end

Examples:

Mock empty Language module


Mock the Language module with the language method


Mock the Language module with language returning a default value




50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'src/ruby/yast/rspec/helpers.rb', line 50

def self.define_yast_module(name, methods: [:fake_method], force: false, &block)
  # sanity check, make sure the code coverage is already running if it is enabled
  if ENV["COVERAGE"] && (!defined?(SimpleCov) || !SimpleCov.running)
    abort "\nERROR: The `define_yast_module` method needs to be called *after* " \
      "enabling the code coverage tracking with `SimpleCov.start`!\n" \
      "  Called from: #{caller(1).first}\n\n"
  end

  # force using the full stubs, useful for testing locally when the modules are installed
  if ENV["YAST_FORCE_MODULE_STUBS"] || force
    define_missing_yast_module(name, methods, &block)
  else
    # try loading the module, it might be present in the system (running locally
    # or in GitHub Actions), mock it only when missing (e.g. in OBS build)
    Yast.import(name)
    puts "Found module Yast::#{name}"
  end
rescue NameError
  define_missing_yast_module(name, methods, &block)
end