Back to Repositories

Validating Entry Model Core Components in DevDocs Documentation System

This test suite validates the core Entry model functionality in the DevDocs documentation system. It ensures proper handling of documentation entries with comprehensive validation of name, path, and type attributes while maintaining data integrity and proper JSON serialization.

Test Coverage Overview

The test suite provides thorough coverage of the Entry class functionality, focusing on object initialization, attribute validation, and comparison operations.

  • Validates entry creation with name, path, and type parameters
  • Tests error handling for nil/empty values
  • Verifies special handling for index paths
  • Ensures proper attribute sanitization

Implementation Analysis

Implements RSpec-style testing using Minitest::Spec for behavior-driven development. The testing approach follows a systematic pattern of validating object construction, attribute manipulation, and equality comparison.

Utilizes let blocks for test setup and employs helper methods for consistent entry creation across test cases.

Technical Details

  • Testing Framework: Minitest::Spec
  • Test Helper Integration: Custom test_helper.rb
  • Custom Assertions: assert_raises, assert_equal, refute_equal
  • Mock Objects: Entry initialization with controlled parameters
  • Error Handling: Docs::Entry::Invalid exception testing

Best Practices Demonstrated

The test suite exemplifies solid testing practices by maintaining single responsibility per test, clear naming conventions, and comprehensive edge case coverage.

  • Isolated test cases with clear purpose
  • Consistent test structure and organization
  • Comprehensive validation scenarios
  • Proper setup and teardown patterns

freecodecamp/devdocs

test/lib/docs/core/models/entry_test.rb

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

class DocsEntryTest < Minitest::Spec
  Entry = Docs::Entry

  let :entry do
    Entry.new('name', 'path', 'type')
  end

  def build_entry(name = 'name', path = 'path', type = 'type')
    Entry.new(name, path, type)
  end

  describe ".new" do
    it "stores #name, #path and #type" do
      entry = Entry.new('name', 'path', 'type')
      assert_equal 'name', entry.name
      assert_equal 'path', entry.path
      assert_equal 'type', entry.type
    end

    it "raises an error when #name, #path or #type is nil or empty" do
      assert_raises(Docs::Entry::Invalid) { Entry.new(nil, 'path', 'type') }
      assert_raises(Docs::Entry::Invalid) { Entry.new('', 'path', 'type') }
      assert_raises(Docs::Entry::Invalid) { Entry.new('name', nil, 'type') }
      assert_raises(Docs::Entry::Invalid) { Entry.new('name', '', 'type') }
      assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', nil) }
      assert_raises(Docs::Entry::Invalid) { Entry.new('name', 'path', '') }
    end

    it "don't raise an error when #path is 'index' and #name or #type is nil or empty" do
      Entry.new(nil, 'index', 'type')
      Entry.new('', 'index', 'type')
      Entry.new('name', 'index', nil)
      Entry.new('name', 'index', '')
    end
  end

  describe "#name=" do
    it "removes surrounding whitespace" do
      entry.name = " \n\rname "
      assert_equal 'name', entry.name
    end

    it "accepts nil" do
      entry.name = nil
      assert_nil entry.name
    end
  end

  describe "#type=" do
    it "removes surrounding whitespace" do
      entry.type = " \n\rtype "
      assert_equal 'type', entry.type
    end

    it "accepts nil" do
      entry.type = nil
      assert_nil entry.type
    end
  end

  describe "#==" do
    it "returns true when the other has the same name, path and type" do
      assert_equal build_entry, build_entry
    end

    it "returns false when the other has a different name" do
      entry.name = 'other_name'
      refute_equal build_entry, entry
    end

    it "returns false when the other has a different path" do
      entry.path = 'other_path'
      refute_equal build_entry, entry
    end

    it "returns false when the other has a different type" do
      entry.type = 'other_type'
      refute_equal build_entry, entry
    end
  end

  describe "#root?" do
    it "returns true when #path is 'index'" do
      entry.path = 'index'
      assert entry.root?
    end

    it "returns false when #path is 'path'" do
      entry.path = 'path'
      refute entry.root?
    end
  end

  describe "#as_json" do
    it "returns a hash with the name, path and type" do
      as_json = Entry.new('name', 'path', 'type').as_json
      assert_instance_of Hash, as_json
      assert_equal [:name, :path, :type], as_json.keys
      assert_equal %w(name path type), as_json.values
    end
  end
end