Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

rspec what is shared examples

# shared_examples are tests written in a way that you can run them in
# multiple settings; extracting common behavior between objects.

it_behaves_like "a correct object remover" do
    ...
end

# shared_contexts is any setup code that you can use to prepare a test case.
# This allows you to include test helper methods or prepare for the tests to run.
include_context "has many users to begin with"

# Example
RSpec.describe TailwindDsl::Etl::ComponentStructures::Generator do
  include_context :use_temp_folder
  include_context :has_configured_uikit
  
  it 'sample' do
    puts temp_folder
    puts uikit
  end
end

# Used to read a complex data object
RSpec.shared_context :has_configured_uikit do
  let(:design_systems_file) { File.join(SPEC_FOLDER, 'samples/input/uikit.json') }
  let(:design_systems_data) { JSON.parse(File.read(design_systems_file), symbolize_names: true) }
  let(:uikit) { ::TailwindDsl::Etl::RawComponents::UiKit.new(design_systems_data) }
end

# Used to create a temporary folder
RSpec.shared_context :use_temp_folder do
  attr_reader :temp_folder

  around do |example|
    Dir.mktmpdir('rspec-') do |folder|
      @temp_folder = folder
      example.run
    end
  end
end
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #rspec #shared #examples
ADD COMMENT
Topic
Name
3+3 =