liveBook · Manning

Value object

As explained in Unit Testing Principles, Practices, and Patterns

A value object is an immutable object identified solely by its content rather than by an individual identity.

A value object is immutable and has no individual identity: its content alone determines what it is. Two instances with matching content are interchangeable, so replacing one with a test double provides no benefit. section 2.2

Use these distinctions when deciding whether a dependency needs substitution:

  • Treat immutable values such as the integer 5 and Product.Shampoo as ordinary inputs; their contents cannot change and their instances do not affect one another. section 2.2
  • Replace mutable private dependencies when isolation requires controlling changing state, but leave private immutable dependencies real. section 2.2
  • Represent functional decisions as values so tests compare outputs instead of configuring mocks or inspecting filesystem interactions. section 6.4

Turning FileUpdate into a value object

AuditManager returns a FileUpdate containing a file name and new content. Defining value equality lets the test assert one expected FileUpdate against the returned result, replacing two property assertions and avoiding reference-based comparison. section 6.4

Table 2.1. The differences between the London and classical schools of unit testing, summed up by the approach to isolation, the size of a unit, and the use of test doubles (view table figure)
 

Isolation of

A unit is

Uses test doubles for

London school Units A class All but immutable dependencies
Classical school Unit tests A class or a set of classes Shared dependencies

Use the dependency-isolation comparison to see why immutable dependencies remain real while mutable or shared dependencies may be replaced. section 2.2

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines identity and immutability, then shows .NET equality implementation; it stops short of broader construction guidance.
Examples
Uses the integer 5, Product.Shampoo, FileUpdate, and audit-file instructions as concrete value examples.
What it assumes
Assumes familiarity with dependencies and unit-test isolation while defining the distinction from the integer 5 example.
What you can do after
Apply value equality to simplify output assertions and avoid substituting immutable dependencies with test doubles.
How it connects
Connects value objects with immutable dependencies, test doubles, unit testing, functional architecture, and output-based testing.

Also explained in

Compare how 3 books explain value object →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.