liveBook · Manning

Value

As explained in Unit Testing Principles, Practices, and Patterns

A value is dependency data that is available from the object's known state and remains unchanged during the operation that consumes it.

A value is dependency data available from an object's known state that remains unchanged during the operation consuming it. Immutable constructor state, such as a maximum-entry limit, qualifies as a value. section 6.5

Distinguish values from collaborators by how the dependency behaves:

  • Treat immutable, already-available data as a value. section 6.5
  • Treat mutable dependencies or proxies for data not yet in memory as collaborators. section 6.5
  • Prefer values in functional-core code because collaborators require external calls and prevent output-based testing. section 6.5

Pass time as a value

Obtain the current time through an explicit service at the start of a business operation, then pass the resulting DateTime value through the domain logic. Prefer this over an ambient context when possible because plain values are easier to use and stub. section 11.6 section 11.8

Listing 11.17. Current date and time as an explicit dependency
public interface IDateTimeServer
{
    DateTime Now { get; }
}

public class DateTimeServer : IDateTimeServer
{
    public DateTime Now => DateTime.Now;
}

public class InquiryController
{
    private readonly IDateTimeServer _dateTimeServer;

    public InquiryController(
        IDateTimeServer dateTimeServer)             #1
    {
        _dateTimeServer = dateTimeServer;
    }

    public void ApproveInquiry(int id)
    {
        Inquiry inquiry = GetById(id);
        inquiry.Approve(_dateTimeServer.Now);      #2
        SaveInquiry(inquiry);
    }
}

Shows a service supplying time to a controller, which passes the resulting value into domain behavior. section 11.6

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Distinguishes immutable data from mutable or external collaborators and stops short of broader performance trade-offs.
Examples
Uses an immutable entry limit and an explicitly passed DateTime as concrete values.
What it assumes
Assumes familiarity with dependencies and distinguishes collaborators by mutability or deferred shared data.
What you can do after
Apply it by replacing ambient or external dependencies with explicit values when operations can receive stable data.
How it connects
Connects values with collaborators, functional architecture, dependency injection, ambient context, and output-based testing.

Also explained in

Compare how 7 books explain value →

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.