Class BuilderBehavior
Factories for helper classes that provide useful test patterns for builder types.
Inheritance
Namespace: LaunchDarkly.TestHelpers
Assembly: LaunchDarkly.TestHelpers.dll
Syntax
public static class BuilderBehavior : Object
Remarks
These helpers make it easier to provide thorough test coverage of builder types. It is easy when implementing builders to make basic mistakes like not setting the right property in a setter, not enforcing desired constraints, or not copying all the properties in a copy constructor.
The general pattern consists of creating a generic helper for the builder type and the type that it builds, then creating a property helper for each settable property and performing standard assertions with it. Example:
// This assumes there is a type MyBuilder whose Build method creates an // instance of MyType, with properties Height and Weight. var tester = BuilderBehavior.For(() => new MyBuilder(), b => b.Build()); var height = tester.Property(x => x.Height, b => (b, value) => b.Height(value)); height.AssertDefault(DefaultHeight); height.AssertCanSet(72); height.AssertSetIsChangedTo(-1, 0); // setter should enforce minimum height of 0 var weight = tester.Property(x => x.Weight, b => (b, value) => b.Weight(value)); weight.AssertDefault(DefaultWeight); weight.AssertCanSet(200); weight.AssertSetIsChangedTo(-1, 0); // setter should enforce minimum weight of 0
It uses Xunit assertion methods.
Methods
For<TBuilder>(Func<TBuilder>)
Provides a generic BuilderBehavior.InternalStateTester<TBuilder> for testing methods of a builder against the builder's own internal state.
Declaration
public static BuilderBehavior.InternalStateTester<TBuilder> For<TBuilder>(Func<TBuilder> constructor)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TBuilder> | constructor | function that constructs a |
Returns
Type | Description |
---|---|
BuilderBehavior.InternalStateTester<TBuilder> | an BuilderBehavior.InternalStateTester<TBuilder> instance |
Type Parameters
Name | Description |
---|---|
TBuilder | the builder type |
Remarks
This can be used in cases where it is not feasible for the test code to actually call the builder's build method, for instance if it has unwanted side effects.
For<TBuilder, TBuilt>(Func<TBuilder>, Func<TBuilder, TBuilt>)
Provides a generic BuilderBehavior.BuildTester<TBuilder, TBuilt> for testing methods of a builder against properties of the type it builds.
Declaration
public static BuilderBehavior.BuildTester<TBuilder, TBuilt> For<TBuilder, TBuilt>(Func<TBuilder> constructor, Func<TBuilder, TBuilt> buildMethod)
Parameters
Type | Name | Description |
---|---|---|
System.Func<TBuilder> | constructor | function that constructs a |
System.Func<TBuilder, TBuilt> | buildMethod | function that creates a |
Returns
Type | Description |
---|---|
BuilderBehavior.BuildTester<TBuilder, TBuilt> |
Type Parameters
Name | Description |
---|---|
TBuilder | the builder type |
TBuilt | the type that it builds |