Back to Repositories

Testing Manifest Generation and Storage Implementation in DevDocs

This test suite validates the functionality of the Manifest class in DevDocs, focusing on file storage, JSON serialization, and metadata handling. It ensures proper document manifest generation and storage for the documentation system.

Test Coverage Overview

The test suite provides comprehensive coverage of the Manifest class functionality, including:

  • File storage operations and path handling
  • JSON manifest generation and serialization
  • Document metadata processing and validation
  • Edge cases for missing meta files
  • Integration with the store interface

Implementation Analysis

The testing approach uses RSpec-style syntax with Minitest, implementing behavior-driven development patterns. Tests utilize mocking and stubbing to isolate components and verify interactions between the Manifest class and store implementations.

Key patterns include:
  • Let blocks for test setup and dependency injection
  • Nested describe blocks for context organization
  • Mock expectations for verifying store interactions
  • JSON structure validation

Technical Details

Testing infrastructure includes:

  • Minitest as the testing framework
  • RSpec-style expectation syntax
  • Mock object framework for dependency isolation
  • Custom NullStore implementation for testing
  • JSON parsing and validation utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Ruby applications:

  • Proper test isolation and dependency management
  • Comprehensive edge case coverage
  • Clear test organization and naming
  • Effective use of test doubles
  • Focused unit tests with single responsibilities

freecodecamp/devdocs

test/lib/docs/core/manifest_test.rb

            
require_relative '../../../test_helper'
require_relative '../../../../lib/docs'

class ManifestTest < Minitest::Spec
  let :doc do
    doc = Class.new Docs::Scraper
    doc.name = 'TestDoc'
    doc.options[:attribution] = 'foo'
    doc
  end

  let :store do
    Docs::NullStore.new
  end

  let :manifest do
    Docs::Manifest.new store, [doc]
  end

  describe "#store" do
    before do
      stub(manifest).as_json
    end

    it "stores a file" do
      mock(store).write.with_any_args
      manifest.store
    end

    describe "the file" do
      it "is named ::FILENAME" do
        mock(store).write Docs::Manifest::FILENAME, anything
        manifest.store
      end

      it "contains the manifest's JSON dump" do
        stub(manifest).to_json { 'json' }
        mock(store).write anything, 'json'
        manifest.store
      end
    end
  end

  describe "#as_json" do
    let :meta_path do
      'meta_path'
    end

    before do
      stub(doc).meta_path { meta_path }
    end

    it "returns an array" do
      manifest = Docs::Manifest.new store, []
      assert_instance_of Array, manifest.as_json
    end

    context "when the doc has a meta file" do
      before do
        stub(store).exist?(meta_path) { true }
        stub(store).read(meta_path) { '{"name":"Test", "db_size": 776533}' }
      end

      it "includes the doc's meta representation" do
        json = manifest.as_json
        assert_equal 1, json.length
        assert_equal "{\"name\"=>\"Test\", \"db_size\"=>776533, :attribution=>\"foo\", :alias=>nil}", json[0].to_s
      end
    end

    context "when the doc doesn't have a meta file" do
      it "doesn't include the doc" do
        stub(store).exist?(meta_path) { false }
        assert_empty manifest.as_json
      end
    end
  end

  describe "#to_json" do
    it "returns the JSON string for #as_json" do
      stub(manifest).as_json { { test: 'ok' } }
      assert_equal "{\n  \"test\": \"ok\"\n}", manifest.to_json
    end
  end
end