From 638522f0e044833721a72c91b40e7e7b531493eb Mon Sep 17 00:00:00 2001 From: Clinton Wolfe Date: Fri, 4 Mar 2022 13:47:56 -0500 Subject: [PATCH] Working unit tests, holey moley Signed-off-by: Clinton Wolfe --- .../unit/inspec-resource-test-template.erb | 20 ++++++++++++++++++- test/helper.rb | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/plugins/inspec-init/templates/resources/tests/unit/inspec-resource-test-template.erb b/lib/plugins/inspec-init/templates/resources/tests/unit/inspec-resource-test-template.erb index 6d332e845..e74620442 100644 --- a/lib/plugins/inspec-init/templates/resources/tests/unit/inspec-resource-test-template.erb +++ b/lib/plugins/inspec-init/templates/resources/tests/unit/inspec-resource-test-template.erb @@ -1 +1,19 @@ -# Some unit test code \ No newline at end of file +# If we can load the InSpec globals definition file... +require "inspec/globals" +# ... we can find the core test unit helper file +require "#{Inspec.src_root}/test/helper" + +# Load (require) the resource library file +# TODO: core path change +require_relative "../../libraries/<%= resource_name %>.rb" + +describe Inspec::Resources::<%= class_name %> do + it "works correctly with the constructor on the platform" do + # Add contructor arguments to load_resource if needed + resource = MockLoader.new("<%= supports_platform %>".to_sym).load_resource("<%= resource_name %>") + + _(resource.has_bells?).must_equal true + _(resource.shoe_size).must_equal 42 + _(resource.resource_id).must_equal "something special" + end +end diff --git a/test/helper.rb b/test/helper.rb index 3c5b8c041..4a3fa0a29 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -68,8 +68,8 @@ require "webmock/minitest" require "mocha/minitest" require "inspec/log" require "inspec/backend" -require "helpers/mock_loader" -require "helpers/resources" +require_relative "helpers/mock_loader" +require_relative "helpers/resources" TMP_CACHE = {} # rubocop: disable Style/MutableConstant