In our last installment, we took a look at using Auto-mocking Containers as a way of reducing coupling and obscurity within our tests. This time, we’ll take a look at another technique which aids in preventing obscurity caused by complex assertions: Custom Assertions.
Custom Assertions
As discussed earlier in the series, executable specifications are a model for our requirements. Whether serving as a guide for our implementation or as documentation for existing behavior, specifications should be easy to understand. Assertion implementation is one of the areas that can often begin to obscure the intent of our specifications. When standard assertions fall short of expressing what is being validated clearly and concisely, they can be replaced with Custom Assertions. Custom assertions are domain-specific assertions which encapsulate complex or obscure testing logic within intention-revealing methods.
Consider the following example which validates that the items returned from a ReviewService class are sorted in descending order:
public class when_a_customer_retrieves_comment_history : WithSubject<ReviewService>
{
const string ItemId = "123";
static IEnumerable<Comment> _comments;
Establish context = () => For<IItemRepository>()
.Setup(x => x.Get(ItemId))
.Returns(new Item(new[]
{
new Comment("comment 1", DateTime.MinValue.AddDays(1)),
new Comment("comment 2", DateTime.MinValue.AddDays(2)),
new Comment("comment 3", DateTime.MinValue.AddDays(3))
}));
Because of = () => _comments = Subject.GetCommentsForItem(ItemId);
It should_return_comments_sorted_by_date_in_descending_order = () =>
{
Comment[] commentsArray = _comments.ToArray();
for (int i = commentsArray.Length - 1; i > 0; i--)
{
if (commentsArray[i].TimeStamp > commentsArray[i - 1].TimeStamp)
{
throw new Exception(
string.Format(
"Expected comments sorted in descending order, but found comment \'{0}\' on {1} after \'{2}\' on {3}",
commentsArray[i].Text, commentsArray[i].TimeStamp, commentsArray[i - 1].Text,
commentsArray[i - 1].TimeStamp));
}
}
};
}
While the identifiers used to describe the specification are clear, the observation’s implementation contains a significant amount of verification logic which makes the specification more difficult to read. By moving the verification logic into a custom assertion which describes what is expected, we can clarify the specification’s intent.
When developing on the .Net platform, Extension Methods provide a nice way of encapsulating assertion logic while achieving an expressive API. The following listing shows the same assertion logic contained within an extension method:
public static class CustomAssertions
{
public static void ShouldBeSortedByDateInDescendingOrder(this IEnumerable<Comment> comments)
{
Comment[] commentsArray = comments.ToArray();
for (int i = commentsArray.Length - 1; i > 0; i--)
{
if (commentsArray[i].TimeStamp > commentsArray[i - 1].TimeStamp)
{
throw new Exception(
string.Format(
"Expected comments sorted in descending order, but found comment \'{0}\' on {1} after \'{2}\' on {3}",
commentsArray[i].Text, commentsArray[i].TimeStamp, commentsArray[i - 1].Text,
commentsArray[i - 1].TimeStamp));
}
}
}
}
Using this new custom assertion, the specification can be rewritten to be more intention-revealing:
public class when_a_customer_retrieves_comment_history : WithSubject<ReviewService>
{
const string ItemId = "123";
static IEnumerable<Comment> _comments;
Establish context = () => For<IItemRepository>()
.Setup(x => x.Get(ItemId))
.Returns(new Item(new[]
{
new Comment("comment 1", DateTime.MinValue.AddDays(1)),
new Comment("comment 2", DateTime.MinValue.AddDays(2)),
new Comment("comment 3", DateTime.MinValue.AddDays(3))
}));
Because of = () => _comments = Subject.GetCommentsForItem(ItemId);
It should_return_comments_sorted_by_date_in_descending_order = () => _comments.ShouldBeSortedByDateInDescendingOrder();
}
Verifying Assertion Logic
Another advantage of factoring out complex assertion logic into custom assertion methods is the ability to verify that the logic actually works as expected. This can be particularly valuable if the assertion logic is reused by many specifications.
The following listing shows positive and negative tests for our custom assertion:
public class when_asserting_unsorted_comments_are_sorted_in_descending_order
{
static Exception _exception;
static List<Comment> _unsortedComments;
Establish context = () =>
{
_unsortedComments = new List<Comment>
{
new Comment("comment 1", DateTime.MinValue.AddDays(1)),
new Comment("comment 4", DateTime.MinValue.AddDays(4)),
new Comment("comment 3", DateTime.MinValue.AddDays(3)),
new Comment("comment 2", DateTime.MinValue.AddDays(2))
};
};
Because of = () => _exception = Catch.Exception(() => _unsortedComments.ShouldBeSortedByDateInDescendingOrder());
It should_throw_an_exception = () => _exception.ShouldBeOfType(typeof(Exception));
}
public class when_asserting_sorted_comments_are_sorted_in_descending_order
{
static Exception _exception;
static List<Comment> _unsortedComments;
Establish context = () =>
{
_unsortedComments = new List<Comment>
{
new Comment("comment 4", DateTime.MinValue.AddDays(4)),
new Comment("comment 3", DateTime.MinValue.AddDays(3)),
new Comment("comment 2", DateTime.MinValue.AddDays(2)),
new Comment("comment 1", DateTime.MinValue.AddDays(1))
};
};
Because of = () => _exception = Catch.Exception(() => _unsortedComments.ShouldBeSortedByDateInDescendingOrder());
It should_not_throw_an_exception = () => _exception.ShouldBeNull();
}
Conclusion
This time, we examined a simple strategy for clarifying the intent of our specifications involving the movement of complex verification logic into custom assertions methods. Next time we’ll take a look at another strategy for clarifying the intent of our specifications which also serves to reduce test code duplication and test-specific code within production.
In my article: Single Action Controllers with ASP.Net MVC, I presented a simple way of designing controllers within the ASP.Net MVC framework that represent discrete actions as opposed to classes which contain actions. This prompted a few questions about the motivations behind this strategy which this article will discuss in more detail.
To explain, we’ll review the topics of Cohesion, Role Stereotypes and the Single Responsibility Principle.
Cohesion
Cohesion is defined as the functional relatedness of the elements of a module. If all the methods on a given object pertain to a related set of operations, the object can be said to have high-cohesion. If an object has a bunch of miscellaneous methods which deal with unrelated concerns, it could be said to have low-cohesion. Cohesion can be further broken down into categories based upon how the responsibilities are related. The responsibilities of a given class may be similar in the sequence of operations they perform, in the dependencies they use, in the business domain concerns they facilitate, etc. What makes an object cohesive depends upon which aspect of relatedness you are measuring.
Role Stereotypes
Role Stereotypes concern the ontology of an object within object-oriented design. Role stereotypes can be thought of as role patterns which help us to rationalize the assignment of roles and responsibilities to objects within an architecture. One set of stereotypes set forth by Rebecca Wirfs-Brock is as follows:
- Information holder – an object designed to know certain information and provide that information to other objects.
- Structurer – an object that maintains relationships between objects and information about those relationships.
- Service provider – an object that performs specific work and offers services to others on demand.
- Controller – an object designed to make decisions and control a complex task.
- Coordinator – an object that doesn’t make many decisions but, in a rote or mechanical way, delegates work to other objects.
- Interfacer – an object that transforms information or requests between distinct parts of a system.
Another set of role stereotypes set forth by Steve Freeman and Nat Pryce as a heuristic for thinking about object roles within the context of collaboration is as follows:
- Dependencies – Services that the object requires from its peers so it can perform its responsibilities.
- Notifications – Peers that need to be kept up to date with the object’s activity.
- Adjustments – Peers that adjust the object’s behavior to the wider needs of the system.
Object stereotypes serve as an aid when considering what roles and responsibilities a particular object should have within a given context, but they shouldn’t be looked to as a set of rules governing how to design. Depending upon what aspect of a design is being considered, objects can serve in different contexts or may fit multiple stereotypes simultaneously. Additionally, stereotypes should not be confused with the responsibilities and characteristics prescribed by formal patterns and pattern participants which may happen to share similar naming. For example, Wirfs-Brock’s Controller stereotype shouldn’t be confused with Controllers described by the Model-View-Controller, Application Controller or Supervising Controller patterns.
Single Responsibility Principle
The Single Responsibility Principle states:
A class should have only one reason to change.
The Single Responsibility Principle seeks to achieve cohesion by grouping concerns based upon the forces which cause them to change together. For example, consider an object whose purpose is to provide caching services to an application. It may have different behaviors for adding, retrieving, and removing items from a cache, but apart from each of these methods being functionally related (i.e. dealing with cache), they would also tend to change together based upon modifications to the underlying caching store implementation (e.g. changing the service to use a caching provider Strategy vs. a local dictionary field). If the caching service were extended to also encrypt the cached items, this could be reasoned to violate the Single Responsibility Principle. While the operations may be cohesive from the perspective of the responsibilities which are uniformly applied to the caching of items, persistence and encryption represent two different axes of change.
While the concept of Cohesion and the Single Responsibility Principle are related, something can be considered cohesive by one measure of relatedness, but have multiple reasons to change. The Single Responsibility Principle measures one type of cohesion: how related the responsibilities of a module are with respect to their axes of change.
Confluence of Principles
So, how do all these abstract concepts relate to how one might decide to implement the Model-View-Controller pattern? Going back to Trygve Reenskaug’s original design of the Model-View-Controller pattern, the Model’s role was to represent real world processes and entities; the View’s role was to provide the visualization of the model; the Controller’s role was to serve as an interface between the end user and the model. The Controller’s responsibilities were achieved by intercepting hardware signals in the form of keyboard and mouse events and translating them to operations upon the Model. As operations upon the Model changed its state, the View would be updated through the use of the Observer Pattern. The Controller’s role in interacting with the View was primarily presentation-specific concerns (navigation, the expanding and collapsing of menus, highlighting, etc.). As the MVC pattern was adapted for use with Web applications, the hardware signals were replaced by HTTP requests and the Controller took a more active role in communicating updates to the View due to the stateless nature of the Web. Though the Controller had to step up its responsibility a bit in this new manifestation of the MVC pattern, its purpose was still that of Model interface.
While the Controller’s primary responsibility is to interpret signals and adapt those signals to meaningful operations upon the Model, the cohesiveness of the operations upon the Controller should be guided by the Single Responsibly Principle within the context of its role stereotype. That is to say, the role of the Controller within the Model-View-Controller pattern is not to mirror the Model’s responsibility in terms of HTTP requests and responses, but to serve as an adaptor whose responsibilities are organized based upon the relatedness of the technical needs to serve in this adapting role. For example, a single Domain object or service may have a number of operations which are grouped based upon their relatedness in representing a domain concept, but invoking the behavior of each may require that completely disparate dependencies be supplied to the Model. One may require an auditing service, another a discount strategy, and still another a caching service. These represent three separate axes of change which could affect the Controller for different reasons. The types of requests and responses represent yet another axis of change, as some requests utilize different protocols (e.g. delegation to a View template engine, JSON, binary stream, etc.)
For simple applications, a single controller may adhere to the Single Responsibility Principle due to the singularity of protocol and dependencies required for the underlying Model it is tasked with interfacing (e.g. basic CRUD applications). As applications grow in complexity, more forces of change begin to be imposed upon the design. Therefore, applying the same sets of principles will lead you to different manifestations of Controllers depending on the style of application you are designing.
Conclusion
In summary, Cohesion is a measure of the relatedness of the elements of a module, but it is only until we provide context to an element’s purpose that we can begin to define cohesion in a meaningful way. By identifying the role stereotype of a component, we can then apply the Single Responsibility Principle to the behaviors of the role based upon the cohesiveness of each element’s reason for change.
In the last installment, I set forth some recommendations for using Test Doubles effectively. In this article, I’ll discuss a class of tools which can aid in reducing some of the coupling and obscurity that comes with the use of Test Doubles: Auto-mocking Containers.
Auto-mocking Containers
Executable specifications can provide valuable documentation of a system’s behavior. When written well, they can not only clearly describe what the system does, but also serve as an example for how the system is intended to be used. Unfortunately, it is this aspect of our specifications which can often end up working against our goal of writing maintainable software.
Ideally, an executable specification would describe the expected behavior of a system in such a way as to also clearly demonstrate it’s intended use without obscuring its purpose with extraneous implementation details. One class of tools which aid in achieving this goal are Auto-mocking Containers.
An Auto-mocking Container is a specialized inversion of control container for constructing a System Under Test with Test Doubles automatically supplied for any dependencies. By using an auto-mocking container, details such as the declaration of test double fields and test double instantiation can be removed from the specification, rendering a cleaner implementation void of such extraneous details.
Consider the following class which displays part details to a user and is responsible for retrieving the details requested form a cached copy if present:
public class DisplayPartDetailsAction
{
readonly ICachingService _cachingService;
readonly IPartDisplayAdaptor _partDisplayAdaptor;
readonly IPartRepository _partRepository;
public DisplayPartDetailsAction(
ICachingService cachingService,
IPartRepository partRepository,
IPartDisplayAdaptor partDisplayAdaptor)
{
_cachingService = cachingService;
_partRepository = partRepository;
_partDisplayAdaptor = partDisplayAdaptor;
}
public void Display(string partId)
{
PartDetail details = _cachingService.RetrievePartDetails(partId) ??
_partRepository.GetPartDetailByPartId(partId);
_partDisplayAdaptor.Display(details);
}
}
The specification for this behavior would need to verify that the System Under Test attempts to retrieve the PartDetail from the ICachingService, but would also need to supply implementations for the IPartRepository and IPartDisplayAdaptor as shown in the following listing:
public class when_displaying_part_details
{
const string PartId = "12345";
static Mock<ICachingService> _cachingServiceMock;
static DisplayPartDetailsAction _subject;
Establish context = () =>
{
_cachingServiceMock = new Mock<ICachingService>();
var partRepositoryDummy = new Mock<IPartRepository>();
var partDisplayAdaptorDummy = new Mock<IPartDisplayAdaptor>();
_subject = new DisplayPartDetailsAction(_cachingServiceMock.Object, partRepositoryDummy.Object,
partDisplayAdaptorDummy.Object);
};
Because of = () => _subject.Display(PartId);
It should_retrieve_the_part_information_from_the_cache =
() => _cachingServiceMock.Verify(x => x.RetrievePartDetails(PartId), Times.Exactly(1));
}
By using an auto-mocking container, the specification can be written without the need of an explicit Mock field, or instantiating Dummy instances for the IPartRepository and IPartDisplayAdaptor dependencies. The following demonstrates such an example using AutoMock, an auto-mocking container which leverages the Moq framework:
public class when_displaying_part_details
{
const string PartId = "12345";
static AutoMockContainer _container;
static DisplayPartDetailsAction _subject;
Establish context = () =>
{
_container = new AutoMockContainer(new MockFactory(MockBehavior.Loose));
_subject = _container.Create<DisplayPartDetailsAction>();
};
Because of = () => _subject.Display(PartId);
It should_retrieve_the_part_information_from_the_cache =
() => _container.GetMock<ICachingService>().Verify(x => x.RetrievePartDetails(PartId), Times.Exactly(1));
}
While this implementation eliminates references to the extraneous dependencies, it does impose a bit of extraneous implementation details of its own. To further relieve this specification of implementation details associated with the auto-mocking container, a reusable base context can be extracted:
public abstract class WithSubject<T> where T : class
{
protected static AutoMockContainer Container;
protected static T Subject;
Establish context = () =>
{
Container = new AutoMockContainer(new MockFactory(MockBehavior.Loose));
Subject = Container.Create<T>();
};
protected static Mock<TDouble> For<TDouble>() where TDouble : class
{
return Container.GetMock<TDouble>();
}
}
By extending the auto-mocking base context, the specification can be written more concisely:
public class when_displaying_part_details : WithSubject<DisplayPartDetailsAction>
{
const string PartId = "12345";
Because of = () => Subject.Display(PartId);
It should_retrieve_the_part_information_from_the_cache =
() => For<ICachingService>().Verify(x => x.RetrievePartDetails(PartId), Times.Exactly(1));
}
Another advantage gained by the use of auto-mocking containers is decoupling. By inverting the concern of how the System Under Test is constructed, dependencies can be added, modified, or deleted without affecting specifications for which the dependency has no bearing.
Trade-offs
While auto-mocking containers can make specifications cleaner, easier to write, and more adaptable to change, their use can come at a slight cost. By using mocking frameworks and hand-rolled doubles directly, there is always at least one point of reference where the requirements of instantiating the System Under Test provides feedback about its design as a whole.
Use of auto-mocking containers allows us to produce contextual slices of how the system works, limiting the information about the system’s dependencies to that knowledge required by the context in question. From a documentation perspective, this can aid in understanding how the system is used to facilitate a particular feature. From a design perspective, however, their use can eliminate one source of feedback about the evolving design of the system. Without such inversion of control, hints of violating the Single Responsibility Principle can be seen within the specifications, evidenced by overly complex constructor initialization. By removing the explicit declaration of the system’s dependencies from the specifications, we also remove this point of feedback.
That said, the benefits of leveraging auto-mocking containers tend to outweigh the cost of removing this point of feedback. Cases of mutually-exclusive dependencies are usually in the minority and each addition and/or modification to a constructor provides an equal level of feedback about a class’s potential complexity.
Conclusion
In this article, we looked at the use of auto-mocking containers as a tool for reducing obscurity and coupling within our specifications. Next time, we’ll look at a technique for reducing the obscurity that comes from overly complex assertions.
In our last installment, the topic of Test Doubles was introduced as a mechanism for verifying and/or controlling the interactions of a component with its collaborators. In this article, we’ll consider a few recommendations for using Test Doubles effectively.
Recommendation 1: Clarify Intent
Apart from guiding the software implementation process and guarding the application’s current behavior against regression, executable specifications (i.e. Automated Tests) serve as the system’s documentation. While well-named specifications can serve to describe what the system should do, we should take equal care in clarifying the intent of how the system’s behavior is verified.
When using test doubles, one simple practice that helps to clarify the verification strategies employed by the specification is to use intention-revealing names for test double instances. Consider the following example which uses the Rhino Mocks framework for creating a Test Stub:
public class when_a_user_views_the_product_detail
{
public const string ProductId = "1";
static ProductDetail _results;
static DisplayOrderDetailCommand _subject;
Establish context = () =>
{
var productDetailRepositoryStub = MockRepository.GenerateStub<IProductDetailRepository>();
productDetailRepositoryStub.Stub(x => x.GetProduct(Arg<string>.Is.Anything))
.Return(new ProductDetail {NumberInStock = 42});
_subject = new DisplayOrderDetailCommand(productDetailRepositoryStub);
};
Because of = () => _results = _subject.QueryProductDetails(ProductId);
It should_display_the_number_of_items_currently_in_stock = () => _results.NumberInStock.ShouldEqual(42);
}
In this example, a Test Stub is created for an IProductDetailRepository type which serves as a dependency for the System Under Test (i.e. the DisplayOrderDetailCommand type). By choosing to explicitly name the Test Double instance with a suffix of “Stub”, this specification communicates that the double serves only to provide indirect input to the System Under Test.
For Rhino Mock users, there are some additional hints in this example which help to indicate that the test double used by this specification is intended to serve as a Test Stub. This includes use of Rhino Mock’s GenerateStub
While this test also indicates that the test double is being used as a Stub by its use of the Rhino Mock framework’s GenerateStub
Recommendation 2: Only Substitute Your Types
Applications often make use of third-party libraries and frameworks. When designing an application which leverages such libraries, there’s often a temptation to substitute types within the framework. Rather than providing a test double for these dependencies, create an abstraction representing the required behavior and provide a test double for the abstraction instead.
There are several issues with providing test doubles for third party components:
First, it precludes any ability to adapt to feedback received from the specification. Since we don’t control the components contained within a third-party libraries, coupling our design to these components limits our ability to guide our designs based upon our interaction with the system through our specifications.
Second, we don’t control when or how the API of third-party libraries may change in future releases. We can exercise some control over when we choose to upgrade to a newer release of a library, but aside from the benefits of keeping external dependencies up to date, there are often external motivating factors outside of our control. By remaining loosely-coupled from such dependencies, we minimize the amount of work it takes to migrate to new versions.
Third, we don’t always have a full understanding of the behavior of third-party libraries. Using test doubles for dependencies presumes that the doubles are going to mimic the behavior of the type they are substituting correctly (at least within the context of the specification). Substituting behavior which you don’t fully understand or control may lead to unreliable specifications. Once a specification passes, there should be no reason for it to ever fail unless you change the behavior of your own code. This can’t be guaranteed when substituting third-party libraries.
While we shouldn’t substitute types from third-party libraries, we should verify that our systems work properly when using third-party libraries. This is achieved through integration and/or acceptance tests. With integration tests, we verify that our systems display the expected behavior when integrated with external systems. If our systems have been properly decoupled from the use of third-party libraries, only the Adaptors need to be tested. A system which has taken measures to remain decoupled from third-party libraries should have far fewer integration tests than those that test the native behavior of the application. With acceptance tests, we verify the behavior of the entire application from end to end which would exercise the system along with its external dependencies.
Recommendation 3: Don’t Substitute Concrete Types
The following principle is set forth in the book Design Patterns: Elements of Reusable Object-Oriented Software by Gamma, et al:
Program to an interface, not an implementation.
All objects possess a public interface and in this sense all object-oriented systems are collaborations of objects interacting through interfaces. What is meant by this principle, however, is that objects should only depend upon the interface of another object, not the implementation of that object. By taking dependencies upon concrete types, objects are implicitly bound by the implementation details of that object. Subtypes can be substituted, but subtypes are inextricably coupled to their base types.
Set forth in the book Agile Software Development, Principles, Patterns, and Practices by Robert C. Martin, a related principle referred to as the Interface Segregation Principe states:
Clients should not be forced to depend on methods that they do not use.
The Interface Segregation Principle is set forth to address several issues that arise from non-cohesive or “fat” interfaces, but the issue most pertinent to our discussion is the problem of associative coupling. When a component takes a dependency upon a concrete type, it forms an associative coupling with all other clients of that dependency. As new requirements drive changes to the internals of the dependency for one client, all other clients coupled directly to the same dependency may be affected regardless of whether they depend upon the same sets of behavior or not. This problem can be mitigated by defining dependencies upon Role-based Interfaces. In this way, objects declare their dependencies in terms of behavior, not specific implementations of behavior.
So, one might ask, “What does this have to do with Test Doubles?” There is nothing particularly problematic about replacing concrete types from an implementation perspective. There’s certainly the issue in some languages of needing to take measures to ensure virtual dispatching can take place thereby allowing the behavior of a concrete type to be overridden, but where this actually becomes relevant to our discussion is in what our specifications are trying to tell us about our design. When you find yourself creating test doubles for concrete types, it’s as if your specifications are crying out: “Hey dummy, you have some coupling here!” By listening to the feedback provided by our specification, we can begin to spot code smells which may point to problems in our implementation.
Recommendation 4: Focus on Behavior
When writing specifications, it can be easy to fall into the trap of over-specifying the components of the system. This occurs when we write specifications that not only verify the expected behavior of a system, but which also verify that the behavior is achieved using a specific implementation.
Writing component-level specifications will always require some level of coupling to the component’s implementation details. Nevertheless, we should strive to minimize the coupling to those interactions which are required to verify the system requirements. If the System Under Test takes 10 steps to achieve the desired outcome, but the outcome of step 10 by itself is sufficient to verify that the desired behavior occurred, our specifications shouldn’t care about steps 1 through 9. If someone figures out a way to achieve the same outcome with only 3 steps, the specifications of the system shouldn’t need to change.
This leads us to a recommendation from the book XUnit Test Patterns: Refactoring Test Code by Gerard Meszaros:
Use the Front Door First
By “front door”, the author means that we should strive to verify behavior using the public interface of our components when possible, and use interaction-based verification when necessary. For example, when the behavior of a component can be verified by checking return values from operations performed by the object or by checking the interactions which occurred with its dependencies, we should prefer checking the return values over checking its interactions. At times, verifying the behavior of an object requires that we examine how the object interacted with its collaborators. When this is necessary, we should strive to remain as loosely coupled as possible by only specifying the minimal interactions required to verify the expected behavior.
Conclusion
In this article, we discussed a few strategies for using Test Doubles effectively. Next time we’ll take a look at a technique for creating Test Doubles which aids in both reducing coupling and obscurity … but at a cost.
In our last installment, we concluded our Test-First example which demonstrated the Test Driven Development process through the creation of a Tic-tac-toe component. When writing automated tests using either a Test-First or classic unit testing approach, it often becomes necessary to verify and/or exercise control over the interactions of a component with its collaborators. In this article, I’ll introduce a family of strategies for addressing these needs, known collectively as Test Doubles. The examples within this article will be presented using the Java programming language.
Doubles
The term “Test Double” was popularized by Gerard Meszaros in his book xUnit Test Patterns. Similar to the role of the “stunt double” in the movie industry in which a leading actor or actress is replaced in scenes requiring a more specialized level of training and/or physical ability, Test Doubles likewise play a substitute role in the orchestration of a System Under Test.
Test Doubles serve two primary roles within automated tests. First, they facilitate the ability to isolate portions of behavior being designed and/or tested from undesired influences of collaborating components. Second, they facilitate the ability to verify the collaboration of one component with another.
Isolating Behavior
There are two primary motivations for isolating the behavior being designed from influences of dependencies: Control and Feedback.
It is often necessary to exercise control over the behavior provided by dependencies of a System Under Test in order to effect a deterministic outcome or eliminate unwanted side-effects. When a real dependency can’t be adequately manipulated for these purposes, test doubles can provide control over how a dependency responds to consuming components.
A second motivation for isolating behavior is to aid in identifying the source of regressions within a system. By isolating a component completely from the behavior of its dependencies, the source of a failing test can more readily be identified when a regression of behavior is introduced.
While test isolation aids in identifying the source of regressions, Extreme Programming (XP) offers an alternative process.
As discussed briefly in the series introduction, XP categories tests as Programmer Tests and Customer Tests rather than the categories of Unit, Integration or Acceptance Tests. One characteristic of Programmer Tests, which differs from classic unit testing, is the lack of emphasis on test isolation. Programmer tests are often written in the form of Component Tests which test subsystems within an application rather than designing/testing the individual units comprising the overall system. One issue this presents is a decreased ability to identify the source of a newly introduced regression based on a failing test due to the fact that the regression may have occurred in any one of the components exercised during the test. Another consequence of this approach is a potential increase in the number of tests which may fail due to a single regression being introduced. Since a single class may be used by multiple subsystems, a regression in behavior of a single class can potentially break the tests for every component which consumes that class.
The strategy used for identifying sources of regressions within a system when writing Programmer Tests is to rely upon knowledge of the last change made within the system. This becomes a non-issue when using emergent design strategies like Test-Driven Development since the addition or modification of behavior within a system tends to happen in very small steps. The XP practice of Pair-Programming also helps to mitigate such issues due to an increase in the number of participants during the design process. Practices such as Continuous Integration and associated check-in guidelines (e.g. The Check-in Dance) also help to mitigate issues with identifying sources of regression. The topic of Programmer Tests will be discussed as a separate topic later in the series.
Verifying Collaboration
To maximize maintainability, we should strive to keep our tests as decoupled from implementation details as possible. Unfortunately, the behavior of a component being designed can’t always be verified through the component’s public interface alone. In such cases, test doubles aid in verifying the indirect outputs of a System Under Test. By replacing a real dependency with one of several test double strategies, the interactions of a component with the double can be verified by the test.
Test Double Types
While a number of variations on test double patterns exist, the following presents the five primary types of test doubles: Stubs, Fakes, Dummies, Spies and Mocks.
Stubs
When writing specifications, the System Under Test often collaborates with dependencies which need to be supplied as part of the setup or interaction stages of a specification. In some cases, the verification of a component’s behavior depends upon providing specific indirect inputs which can’t be controlled by using real dependencies. Test doubles which serve as substitutes for controlling the indirect input to a System Under Test are known as Test Stubs.
The following example illustrates the use of a Test Stub within the context of a package shipment rate calculator specification. In this example, a feature is specified for a shipment application to allow customers to inquire about rates based upon a set of shipment details (e.g. weight, contents, desired delivery time, etc.) and a base rate structure (flat rate, delivery time-based rate, etc.).
In the following listing, a RateCalculator has a dependency upon an abstract BaseRateStructure implementation which is used to calculate the actual rate:
public class RateCalculator {
private BaseRateStructure baseRateStructure;
private ShipmentDetails shipmentDetails;
public RateCalculator(BaseRateStructure baseRateStructure, ShipmentDetails shipmentDetails) {
this.baseRateStructure = baseRateStructure;
this.shipmentDetails = shipmentDetails;
}
public BigDecimal CalculateRateFor(ShipmentDetails shipmentDetails) {
BigDecimal rate = baseRateStructure.calculateRateFor(shipmentDetails);
// other processing ...
return rate;
}
}
The following shows the BaseRateStructure contract which defines a method that accepts shipment details and returns a rate:
public abstract class BaseRateStructure {
public abstract BigDecimal calculateRateFor(ShipmentDetails shipmentDetails);
}
To ensure a deterministic outcome, the specification used to drive the feature’s development can substitute a BaseRateStrctureStub which will always return the configured value:
public class RateCalculatorSpecifications {
public static class when_calculating_a_shipment_rate extends ContextSpecification {
static Reference<BigDecimal> rate = new Reference<BigDecimal>(BigDecimal.ZERO);
static ShipmentDetails shipmentDetails;
static RateCalculator calculator;
Establish context = new Establish() {
public void execute() {
shipmentDetails = new ShipmentDetails();
BaseRateStructure baseRateStructureStub = new BaseRateStructureStub(10.0);
calculator = new RateCalculator(baseRateStructureStub, shipmentDetails);
}
};
Because of = new Because() {
protected void execute() {
rate.setValue(calculator.CalculateRateFor(shipmentDetails));
}
};
It should_return_the_expected_rate = assertThat(rate).isEqualTo(new BigDecimal(10.0));
}
}
For this specificaiton, the BaseRateStructureStub merely accepts a value as a constructor parameter and returns the value when the calculateRateFor() method is called:
public class BaseRateStructureStub extends BaseRateStructure {
BigDecimal value;
public BaseRateStructureStub(double value) {
this.value = new BigDecimal(value);
}
public BigDecimal calculateRateFor(ShipmentDetails shipmentDetails) {
return value;
}
}
Fakes
While it isn’t always necessary to control the indirect inputs of collaborating dependencies to ensure a deterministic outcome, some real components may have other undesired side-effects which make their use prohibitive. For example, components which rely upon an external data store for persistence concerns can significant impact the speed of a test suite which tends to discourage frequent regression testing during development. In cases such as these, a lighter-weight version of the real dependency can be substituted which provides the behavior needed by the specification without the undesired side-effects. Test doubles which provide a simplified implementation of a real dependency for these purposes are referred to as Fakes.
In the following example, a feature is specified for an application serving as a third-party distributor for the sale of tickets to a local community arts theatre to display the itemized commission amount on the receipt. The theatre provides a Web service which handles the payment processing and ticket distribution process, but does not provide a test environment for vendors to use for integration testing purposes. To test the third-party application’s behavior without incurring the side-effects of using the real Web service, a Fake service can be substituted in its place.
Consider that the theatre’s service interface is as follows:
public abstract class TheatreService {
public abstract TheatreReceipt ProcessOrder(TicketOrder ticketOrder);
public abstract CancellationReceipt CancelOrder(int orderId);
// other methods ...
}
To provide the expected behavior without the undesired side-effects, a fake version of the service can be implemented:
public class TheatreServiceFake extends TheatreService {
// private field declarations used in light implementation ...
public TheatreReceipt ProcessOrder(TicketOrder ticketOrder) {
// light implementation details ...
TheatreReceipt receipt = createReceipt();
return new TheatreReceipt();
}
public CancellationReceipt CancelOrder(int orderId) {
// light implementation details ...
CancellationReceipt receipt = createCancellationReceipt();
return receipt;
}
// private methods …
}
The fake service may then be supplied to a PaymentProcessor class within the set up phase of the specification:
public class PaymentProcessorSpecifications {
public static class when_processing_a_ticket_sale extends ContextSpecification {
static Reference<BigDecimal> receipt = new Reference<BigDecimal>(BigDecimal.ZERO);
static PaymentProcessor processor;
Establish context = new Establish() {
protected void execute() {
processor = new PaymentProcessor(new TheatreServiceFake());
}
};
Because of = new Because() {
protected void execute() {
receipt.setValue(processor.ProcessOrder(new Order(1)).getCommission());
}
};
It should_return_a_receipt_with_itemized_commission =
assertThat(receipt).isEqualTo(new BigDecimal(1.00));
}
}
Dummies
There are times when a dependency is required in order to instantiate the System Under Test, but which isn’t required for the behavior being designed. If use of the real dependency is prohibitive in such a case, a Test Double with no behavior can be used. Test Doubles which serve only to provide mandatory instances of dependencies are referred to as Test Dummies.
The following example illustrates the use of a Test Dummy within the context of a specification for a ShipmentManifest class. The specification concerns verification of the class’ behavior when adding new packages, but no message exchange is conducted between the manifest and the package during execution of the addPackage() method.
public class ShipmentManifestSpecifications {
public static class when_adding_packages_to_the_shipment_manifest extends ContextSpecification {
static private ShipmentManifest manifest;
Establish context = new Establish() {
protected void execute() {
manifest = new ShipmentManifest();
}
};
Because of = new Because() {
protected void execute() {
manifest.addPackage(new DummyPackage());
}
};
It should_update_the_total_package_count = new It() {
protected void execute() {
assert manifest.getPackageCount() == 1;
}
};
}
}
Test Spies
In some cases, a feature requires collaborative behavior between the System Under Test and its dependencies which can’t be verified through its public interface. One approach to verifying such behavior is to substitute the associated dependency with a test double which stores information about the messages received from the System Under Test. Test doubles which record information about the indirect outputs from the System Under Test for later verification by the specification are referred to as Test Spies.
In the following example, a feature is specified for an online car sales application to keep an audit trail of all car searches. This information will be used later to help inform purchases made at auction sales based upon which makes, models and price ranges are the most highly sought in the area.
The following listing contains the specification which installs the Test Spy during the context setup phase and examines the state of the Test Spy in the observation stage:
public class SearchServiceSpecifications {
public static class when_a_customer_searches_for_an_automobile extends ContextSpecification {
static AuditServiceSpy auditServiceSpy;
static SearchService searchService;
Establish context = new Establish() {
protected void execute() {
auditServiceSpy = new AuditServiceSpy();
searchService = new SearchService(auditServiceSpy);
}
};
Because of = new Because() {
protected void execute() {
searchService.search(new MakeSearch("Ford"));
}
};
It should_report_the_search_to_the_audit_service = new It() {
protected void execute() {
assert auditServiceSpy.WasSearchCalledOnce() == true : "Expected true, but was false.";
}
};
}
}
For this specification, the Test Spy is implemented to simply increment a private field each time the recordSearch() method is called, allowing the specification to then call the WasSearchCalledOnce() method in an observation to verify the expected behavior:
public class AuditServiceSpy extends AuditService{
private int calls;
public boolean WasSearchCalledOnce() {
return calls == 1;
}
public void recordSearch(Search criteria) {
calls++;
}
}
Mocks
Another technique for verifying the interaction of a System Under Test with its dependencies is to create a test double which encapsulates the desired verification within the test double itself. Test Doubles which validate the interaction between a System Under Test and the test double are referred to as Mocks.
Mock validation falls into two categories: Mock Stories and Mock Observations.
Mock Stories
Mock Stories are a scripted set of expected interactions between the Mock and the System Under Test. Using this strategy, the exact set of interactions are accounted for within the Mock object. Upon executing the specification, any deviation from the script results in an exception.
Mock Observations
Mock Observations are discrete verifications of individual interactions between the Mock and the System Under Test. Using this strategy, the interactions pertinent to the specification context are verified during the observation stage of the specification.
Mock Observations and Test Spies
The use of Mock Observations in practice looks very similar to the use of Test Spies. The distinction between the two is whether a method is called on the Mock to assert that a particular interaction occurred or whether state is retrieved from the Test Spy to assert that a particular interaction occurred.
To illustrate the concept of Mock objects, the following shows the previous example implemented using a Mock Observation instead of a Test Spy.
In the following listing, a second specification is added to the previous SearchServiceSpecifications class which replaces the use of the Test Spy with a Mock:
public class SearchServiceSpecifications {
...
public static class when_a_customer_searches_for_an_automobile_2 extends ContextSpecification {
static AuditServiceMock auditServiceMock;
static SearchService searchService;
Establish context = new Establish() {
protected void execute() {
auditServiceMock = new AuditServiceMock();
searchService = new SearchService(auditServiceMock);
}
};
Because of = new Because() {
protected void execute() {
searchService.search(new MakeSearch("Ford"));
}
};
It should_report_the_search_to_the_audit_service = new It() {
protected void execute() {
auditServiceMock.verifySearchWasCalledOnce();
}
};
}
}
The Mock implementation is similar to the Test Spy, but encapsulates the assert call within the verifySearchWasCalledOnce() method rather than returning the recorded state for the specification to assert:
public class AuditServiceMock extends AuditService {
private int calls;
public void verifySearchWasCalledOnce() {
assert calls == 1;
}
public void recordSearch(Search criteria) {
super.recordSearch(criteria);
calls++;
}
}
While both the Mock Observation and Mock Story approaches can be implemented using custom Mock classes, it is generally easier to leverage a Mocking Framework.
Mocking Frameworks
A Mocking Framework is testing library written to facilitate the creation of Test Doubles with programmable expectations. Rather than writing a custom Mock object for each unique testing scenario, Mock frameworks allow the developer to specify the expected interactions within the context setup phase of the specification.
To illustrate the use of a Mocking Framework, the following listing presents the previous example implemented using the Java Mockito framework rather than a custom Mock object:
public class SearchServiceSpecifications {
...
public static class when_a_customer_searches_for_an_automobile_3 extends ContextSpecification {
static AuditService auditServiceMock;
static SearchService searchService;
Establish context = new Establish() {
protected void execute() {
auditServiceMock = mock(AuditService.class);
searchService = new SearchService(auditServiceMock);
}
};
Because of = new Because() {
protected void execute() {
searchService.search(new MakeSearch("Ford"));
}
};
It should_report_the_search_to_the_audit_service = new It() {
protected void execute() {
verify(auditServiceMock).recordSearch(any(Search.class));
}
};
}
}
In this example, the observation stage of the specification uses Mockito’s static verify() method to assert that the recordSearch() method was called with any instance of the Search class.
In many circumstances, messages are exchanged between a System Under Test and its dependencies. For this reason, Mock objects often need to return stub values when called by the System Under Test. As a consequence, most mocking frameworks can be used to also create Test Doubles whose role is only to serve as a Test Stub. Mocking frameworks which facilitate Mock Observations can also be used to easily create Test Dummies.
Conclusion
In this article, the five primary types of Test Doubles were presented: Stubs, Fakes, Dummies, Spies, and Mocks. Next time, we’ll discuss strategies for using Test Doubles effectively.
In the last installment of our series, we continued our Test-First example by addressing issues filed by the QA team. While I thought we had covered the reported defects pretty well, I wanted to do a little smoke testing against the full application to ensure we hadn’t missed anything. It’s probably good that I did, because I ended up finding one last case that didn’t met the original requirements.
In the course of my testing, I discovered that there was an additional way for a player to beat the game by setting up multiple winning paths. Here’s the steps I took:
In the depicted steps, my first move was to choose the right edge of the board. This happens to be a strategy the articles I consulted with advised against and for which no counter strategy was provided. By choosing a second position which avoided triggering the game’s existing defensive strategies, I was able to set up multiple winning paths by countering the next two choices by the game. The game should be able to counter this strategy by blocking at intersections, so let’s fix this one last issue.
First, let’s start with a new test which describes the behavior we want:
[TestClass]
public class When_the_player_has_two_paths_which_intersect_at_an_available_position
{
[TestMethod]
public void it_should_select_the_intersecting_position()
{
}
}
Next, let’s setup the context and assertion that reflects the way I was able to beat the game:
[TestClass]
public class When_the_player_has_two_paths_which_intersect_at_an_available_position
{
[TestMethod]
public void it_should_select_the_intersecting_position()
{
GameAdvisor gameAdvisor = new GameAdvisor();
var selection = gameAdvisor.WithLayout("OXO\0\0XX\0\0").SelectBestMoveForPlayer('O');
Assert.AreEqual(8, selection);
}
}
Now, let’s run our test suite:
When_the_player_has_two_paths_which_intersect_at_an_available_position Failed it_should_select_the_intersecting_position Assert.AreEqual failed. Expected:<8>. Actual:<9>.
Let’s get this to pass quickly by returning the expected position for this exact layout:
class PositionSelector : IPositionSelector
{
…
public int SelectBestMoveForPlayer(char player)
{
if (_layout == "OXO\0\0XX\0\0")
return 8;
return GetPositionThreateningPlayer(player) ??
GetNextWinningMoveForPlayer(player) ??
Enumerable.Range(1, 9).First(position => _layout[position - 1] == Game.EmptyValue);
}
...
}
To remove the duplication of the layout, let’s start taking small steps toward a final solution. First, let’s create a new DefensiveStrategy for dealing with positions that might allow the opponent to set up multiple winning paths:
class PositionSelector : IPositionSelector
{
…
int? GetPositionThreateningPlayer(char player)
{
return new DefensiveStrategy[]
{
PathCompletionStrategy,
SimpleBlockStrategy,
FirstMoveCounterCenterStrategy,
SecondMoveDiagonalCounterStrategy,
MultiPathCounterStrategy
}
.Select(strategy => strategy(player)).FirstOrDefault(p => p.HasValue);
}
int? MultiPathCounterStrategy(char player)
{
if (_layout == "OXO\0\0XX\0\0")
return 8;
return null;
}
...
}
Next, let’s work through the steps we’ll need to arrive at this value. First, we’ll need to get a list of all the available paths for the opponent:
int? MultiPathCounterStrategy(char player)
{
char opponentValue = GetOpponentValue(player);
List<int[]> opponentPaths = GetAvailablePathsFor(opponentValue);
if (_layout == "OXO\0\0XX\0\0")
return 8;
return null;
}
Next, we want to filter this list down to the paths the opponent has already started:
int? MultiPathCounterStrategy(char player)
{
char opponentValue = GetOpponentValue(player);
List<int[]> opponentPaths = GetAvailablePathsFor(opponentValue);
IEnumerable startedPaths =
opponentPaths.Where(path => new string(path.Select(p => _layout[p - 1]).ToArray())
.Count(value => value == opponentValue) >= 1);
if (_layout == "OXO\0\0XX\0\0")
return 8;
return null;
}
Lastly, we need to compare all the paths to each other, find the ones that have positions in common, and pick the position in common for the first pair. Since we’ll be calling this after our other strategy for dealing with multiple winning paths as the result of the opponent choosing opposite corners, this should only ever find one pair. This logic seems a little more complicated, so I’m going to write it in LINQ rather than using extension methods this time:
int? MultiPathCounterStrategy(char player)
{
char opponentValue = GetOpponentValue(player);
List<int[]> opponentPaths = GetAvailablePathsFor(opponentValue);
IEnumerable startedPaths =
opponentPaths.Where(path => new string(path.Select(p => _layout[p - 1]).ToArray())
.Count(value => value == opponentValue) >= 1);
return (from path in startedPaths
from position in path
from otherPath in startedPaths.SkipWhile(otherPath => otherPath == path)
from otherPosition in otherPath
where otherPosition == position
where _layout[position - 1] == Game.EmptyValue
select new int?(position)).FirstOrDefault();
}
Let’s run the tests again and see what happens:
It passes! We can now release the new version of our component to be integrated into the next build.
Conclusion
We’ve finally come to the conclusion of our Test-First example. Along the way, we followed the Test-Driven Development practice of writing a failing test first, making the test pass as quickly as possible, and refactoring to remove duplication and to clarify intent. When we wrote a failing test, we made sure each test was properly verifying the expected results before attempting to add new behavior to the system. To get our tests passing quickly, we used obvious implementations when we were confident about our approach and felt we could achieve it quickly, but used fake implementations when we weren’t as confident or felt the implementation might take some time. When we wrote a new test to capture new requirements that were already present in the system, we temporarily disabled the behavior of the system to ensure our tests were validating the expected behavior correctly. Throughout our effort, we weren’t afraid to take small steps and strove to do simple things.
While we made some mistakes along the way and discovered opportunities for further improvement, using the Test Driven Development approach aided in our ability to produce working, maintainable software that matters.
Next time, we’ll discuss concepts and strategies for writing tests for collaborating components.
In part 8 of our series, we continued with our Test-First example by addressing issues filed by the UI team. To conclude our example, we’ll finish the remaining issues this time.
Here’s what we have left:
| Issue | Description | Owner |
|---|---|---|
| Defect | The player can always win by choosing positions 1, 5, 2, and 8. The game should prevent the player from winning. | |
| Defect | The game throws an InvalidOperationException when choosing positions 1, 2, 5, and 9. | QA Team |
| Defect | The game makes a move after the player wins. | QA Team |
| Defect | After letting the game win by choosing positions 4, 7, 8, and 6, choosing the last position of 3 throws an InvalidOperationException. | QA Team |
| Defect | When trying to let the game win by choosing positions 1, 7, and 8, the game chose positions 4, 5, and 9 instead of completing the winning sequence 4, 5, 6. | QA Team |
Let’s get started with the first one:
| Issue | Description | Owner |
|---|---|---|
| Defect | The player can always win by choosing positions 1, 5, 2, and 8. The game should prevent the player from winning. |
This sounds like our game isn’t blocking correctly. After some analysis, the problem appears to be that certain strategies can lead to multiple winning choices which aren’t handled by our blocking strategy. Our game was designed to block when the player’s next move could result in a win, but it wasn’t designed to guard against moves that might lead to multiple winning paths.
After doing some research, I discovered several websites that discuss the defensive strategies a player should take when playing Tic-tac-toe. While the sites I found spell out each step in detail, I think I’ve condensed the rules we need down to the following:
- When selecting your first position, always choose a corner or the center position. When the opponent goes first and has chosen either a corner or the center, choose the alternate of the two choices.
- If the player’s second move aligns two of their corners diagonally, choose an edge
- When you don’t need to block, prefer corners to edges.
In theory, adding these strategies to our game would mean that a player would never be able to win, so I confirmed that this was indeed what the customer intended by the original requirements. Based on that information, let’s get started.
We already have a context for describing the behavior that is expected when the game goes first, so let’s review our existing test:
[TestClass] public class When_the_game_goes_first { [TestMethod] public void it_should_put_an_X_in_one_of_the_available_positions() { var game = new Game(); game.GoFirst(); Assert.IsTrue(Enumerable.Range(1, 9).Any(position => game.GetPosition(position).Equals('X'))); } }
This specification says that the game should put an ‘X’ in one of the available positions. What we now want it to do is to put an ‘X’ in center or one of the corner positions. Therefore, we’ll change the name of the test. This test was also written before we created our GameAdvisor, so let’s change the System Under Test to that as well:
[TestClass] public class When_the_game_goes_first { [TestMethod] public void it_should_put_an_X_in_a_corner_or_the_center() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("\0\0\0\0\0\0\0\0\0").SelectBestMoveForPlayer('X'); Assert.IsTrue(new[]{1, 3, 5, 7, 9}.Any(position => position == selection)); } }
Our test passes, but let’s make sure it’s actually verifying the behavior correctly by changing the GameAdvisor to always return the second position:
public int SelectBestMoveForPlayer(char player) { return 2; return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
When_the_game_goes_first Failed it_should_put_an_X_in_a_corner_or_the_center Assert.IsTrue failed.
Our test appears to be working correctly, so we’ll put the code back like it was.
The practice of breaking a passing test to watch it fail is a useful strategy for ensuring we aren’t writing self-passing tests (i.e. tests that always pass due to a defect in the test implementation), or to verify that the test communicates regression in a clear way.
Should we keep tests which pass unexpectedly? While it’s good to delete tests which describe behavior that is no longer applicable or which is explicitly or implicitly covered by another test, we should keep tests which describe important behavior that is coincidentally facilitated by the system. While our GameAdvisor happens to meet our revised specification, it does so because the order of our recommended paths happened to coincide with this requirement, not because anything required it to do so. Therefore, we should keep this test, both because we want to guard against this behavior changing and because it serves as useful documentation of the system’s expectations.
Next, let’s create a test which describes what the game’s first selection should be if a corner is already occupied:
[TestClass] public class When_the_game_selects_its_first_position_where_a_corner_is_occupied { [TestMethod] public void it_should_choose_the_center() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("X\0\0\0\0\0\0\0\0").SelectBestMoveForPlayer('O'); Assert.AreEqual(5, selection); } }
Running this test results in the following:
When_the_game_selects_its_first_position_where_a_corner_is_occupied Failed it_should_choose_the_center Assert.AreEqual failed. Expected:<5>. Actual:<4>.
Since I’m not quite sure how best to proceed, I’m going to take the easy route and modify the SelectBestMoveForPlayer() method to return a 5 when the layout matches the one we’re testing for:
public int SelectBestMoveForPlayer(char player) { if (_layout == "X\0\0\0\0\0\0\0\0") return 5; return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
Now let’s work on a real implementation. Since selecting the middle position based on the opponent’s choices is a defensive move, it sounds like the logic for determining this behavior belongs to the GetPositionThreateningPlayer() method. Let’s modify this method to select the middle position if the opponent has a corner and this is the first move for the player being advised:
int? GetPositionThreateningPlayer(char player) { char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string(path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath.First(position => opponentLayout[position - 1] == '\0'); } if (_layout.Count(position => position == player) == 0 && new[] {0, 2, 6, 8}.Any(position => opponentLayout[position] == 'T')) { return 5; } return null; }
Now let’s refactor. While I don’t see any real duplication, I think our code would be more descriptive if we encapsulate these two paths to a list of “defensive strategies”:
class PositionSelector : IPositionSelector { ... int? GetPositionThreateningPlayer(char player) { return new DefensiveStrategy[] { SimpleBlockStrategy, FirstMoveCounterCenterStrategy } .Select(strategy => strategy(player)).FirstOrDefault(p => p.HasValue); } int? SimpleBlockStrategy(char player) { char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string(path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath.First(position => opponentLayout[position - 1] == '\0'); } return null; } int? FirstMoveCounterCenterStrategy(char player) { int? value = null; char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); if (_layout.Count(position => position == player) == 0 && new[] {0, 2, 6, 8}.Any(position => opponentLayout[position] == 'T')) { value = 5; } return value; } ... delegate int? DefensiveStrategy(char player); }
Now we have a bit of duplication for determining the opponent’s value, so let’s factor that out:
int? SimpleBlockStrategy(char player) { char opponentValue = GetOpponentValue(player); string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string(path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath.First(position => opponentLayout[position - 1] == '\0'); } return null; } int? FirstMoveCounterCenterStrategy(char player) { int? value = null; string opponentLayout = _layout.Replace(GetOpponentValue(player), 'T'); if (_layout.Count(position => position == player) == 0 && new[] {0, 2, 6, 8}.Any(position => opponentLayout[position] == 'T')) { value = 5; } return value; } static char GetOpponentValue(char player) { return (player == 'X') ? 'O' : 'X'; }
While reviewing this code, I noticed that neither of these methods, nor the GetNextWinningMoveForPlayer() method appear to require the opponentLayout string to be created. This appears to be an artifact left over from a much earlier refactoring that somehow went unnoticed. Let’s go ahead and remove the use of this variable and replace the generic token character ‘T’ we were using with the actual opponent value:
int GetNextWinningMoveForPlayer(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => _layout[p - 1] == player)).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); } ... int? SimpleBlockStrategy(char player) { char opponentValue = GetOpponentValue(player); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string(path.Select(p => _layout[p - 1]).ToArray()) .Count(c => c == opponentValue) == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath.First(position => _layout[position - 1] == '\0'); } return null; } int? FirstMoveCounterCenterStrategy(char player) { int? value = null; if (_layout.Count(position => position == player) == 0 && new[] {0, 2, 6, 8}.Any(position => _layout[position] == GetOpponentValue(player))) { value = 5; } return value; }
Let’s move on to the alternate first move strategy: Choosing the corner if the opponent has chosen the center:
[TestClass] public class When_the_game_selects_its_first_position_where_the_center_is_occupied { [TestMethod] public void it_should_choose_a_corner() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("\0\0\0\0X\0\0\0\0").SelectBestMoveForPlayer('O'); Assert.IsTrue(new[] {1, 3, 7, 9}.Any(position => position == selection)); } }
This test already passes, so let’s make sure we’ve written it correctly:
public int SelectBestMoveForPlayer(char player) { return 1; return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
When_the_game_selects_its_first_position_where_the_center_is_occupied Failed it_should_choose_a_corner Assert.IsTrue failed.
The test looks good, so let’s put things back:
Our next defensive strategy is to chose an edge position (i.e. a non-corner, non-center position) if the player’s second move aligns two of their corners diagonally. This strategy prevents one of the ways an opponent can set up two non-diagonal winning paths. Here’s our test:
[TestClass] public class When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners { [TestMethod] public void it_should_choose_an_edge() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("\0\0X\0O\0X\0\0").SelectBestMoveForPlayer('O'); Assert.IsTrue(new[] { 2, 4, 6, 8 }.Any(position => position == selection)); } }
Interestingly, this test also already passes. This must be due to the fact that our GameAdvisor always selects position 4 as its choice if the first row is occupied. It’s possible that the behavior of the GameAdvisor could change in the future in such a way as to allow this condition to be met but not a horizontal alignment in the opposite direction, so let’s change this test to guard against both conditions:
[TestClass] public class When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners { [TestMethod] public void it_should_choose_an_edge() { var gameAdvisor = new GameAdvisor(); new[] { "\0\0X\0O\0X\0\0", "X\0\0\0O\0\0\0X" }.ToList().ForEach(layout => { int selection = gameAdvisor.WithLayout(layout).SelectBestMoveForPlayer('O'); Assert.IsTrue(new[] { 2, 4, 6, 8 }.Any(position => position == selection)); }); } }
Let’s make sure the test is written correctly for both conditions:
public int SelectBestMoveForPlayer(char player) { if (_layout == "\0\0X\0O\0X\0\0" || _layout == "X\0\0\0O\0\0\0X") return 1; return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners Failed it_should_choose_an_edge Assert.IsTrue failed.
This seems to work, so let’s revert it:
Our last change for improving our defensive strategy is to modify the game to prefer corners to edges when we don’t need to block. This prevents a situation where an opponent can align one diagonal path and either a horizontal or vertical winning path. Here’s our test:
[TestClass] public class When_a_game_selects_an_offensive_position { [TestMethod] public void it_should_prefer_corners_to_edges() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("\0\0X\0X\0O\0\0").SelectBestMoveForPlayer('O'); Assert.IsTrue(new[] {1, 9}.Any(position => position == selection)); } }
When_a_game_selects_an_offensive_position Failed it_should_prefer_corners_to_edges Assert.IsTrue failed.
To make this pass, we should only have to rearrange our winning positions array to put the edges as the last position selected and to move any paths with a first or second edge position to the bottom of the list:
class PositionSelector : IPositionSelector { static readonly int[][] _winningPositions = new[] { new[] {1, 3, 2}, new[] {7, 9, 8}, new[] {1, 7, 4}, new[] {3, 9, 6}, new[] {1, 9, 5}, new[] {3, 7, 5}, new[] {2, 8, 5}, new[] {4, 6, 5} }; … }
Let’s run our test to see what happens:
When_a_game_selects_an_offensive_position Failed it_should_choose_an_edge Assert.IsTrue failed.
Our theory seems to have held up, but we ended up breaking our last test. Let’s review the broken test:
[TestClass] public class When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners { [TestMethod] public void it_should_choose_an_edge() { var gameAdvisor = new GameAdvisor(); new[] { "\0\0X\0O\0X\0\0", "X\0\0\0O\0\0\0X" }.ToList().ForEach(layout => { int selection = gameAdvisor.WithLayout(layout).SelectBestMoveForPlayer('O'); Assert.IsTrue(new[] {2, 4, 6, 8}.Any(position => position == selection)); }); } }
Unfortunately, that test represents multiple layouts, so we can’t tell exactly which layout failed. Before proceeding further, let’s see if we can address this problem.
Perhaps the most straightforward way of addressing this issue would be to make two separate tests for each of these conditions, but from a documentation perspective I think it’s more clear to have one test that concerns what to do when the player chooses opposite diagonal corners rather than two describing each of the cases. Viewed in isolation, it may not be as clear that both really represent the same strategy for a different orientations of the board. Let’s stay with our existing approach for this test, but modify it so the exception tells us exactly what scenario is causing an issue. We can achieve this by adding a description to our assertion and aggregating any exception messages together to display once all the cases have been run. We’ll use a exception test helper to cut down on the try/catch noise:
[TestClass] public class When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners { [TestMethod] public void it_should_choose_an_edge() { var gameAdvisor = new GameAdvisor(); var exceptions = new List<string>(); new[] { "\0\0X\0O\0X\0\0", "X\0\0\0O\0\0\0X" }.ToList().ForEach(layout => { int selection = gameAdvisor.WithLayout(layout).SelectBestMoveForPlayer('O'); var exception = Catch.Exception(() => Assert.IsTrue(new[] {2, 4, 6, 8}.Any(position => position == selection), string.Format("edge not selected for layout:{0}", layout))); if(exception != null) exceptions.Add(exception.Message); }); if (exceptions.Count > 0) throw new AssertFailedException(string.Join(Environment.NewLine, exceptions)); } } public static class Catch { public static Exception Exception(Action action) { try { action(); } catch (Exception e) { return e; } return null; } }
Let’s run our tests again:
When_the_game_selects_its_second_position_where_the_player_chooses_opposite_diagonal_corners Failed it_should_choose_an_edge Assert.IsTrue failed. edge not selected for layout:\0\0X\0O\0X\0\0 Assert.IsTrue failed. edge not selected for layout:X\0\0\0O\0\0\0X
This produces the result I was looking for, but the test seems a little obscure. Let’s leave it like this for now, but we’ll discuss techniques for cleaning this up later in our series.
Getting back to the main issue, this test is failing because the GameAdvisor was fulfilling this specification by relying upon the ordering of the original winning patterns array. This was a perfectly acceptable strategy at the time, but we’ll need to add new behavior now that we’ve changed how this works internally.
To get the test passing, let’s approach this just like we would a new failing test and implement the quickest solution that gets the test passing:
public int SelectBestMoveForPlayer(char player) { if (_layout == "\0\0X\0O\0X\0\0" || _layout == "X\0\0\0O\0\0\0X") return 4; return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
Next, let’s create a new DefensiveStrategy method and move our fake implementation to our new method:
int? GetPositionThreateningPlayer(char player) { return new DefensiveStrategy[] { SimpleBlockStrategy, FirstMoveCounterCenterStrategy, SecondMoveDiagonalCounterStrategy } .Select(strategy => strategy(player)).FirstOrDefault(p => p.HasValue); } int? SecondMoveDiagonalCounterStrategy(char player) { if (_layout == "\0\0X\0O\0X\0\0" || _layout == "X\0\0\0O\0\0\0X") return 4; return null; }
Now, let’s change our comparison to only check the positions we care about:
int? SecondMoveDiagonalCounterStrategy(char player) { var opponentValue = GetOpponentValue(player); if((_layout[2] == opponentValue && _layout[6] == opponentValue) || (_layout[0] == opponentValue && _layout[8] == opponentValue)) return 4; return null; }
Lastly, we’ll change the the value of 4 to be the value of the first unoccupied edge, or null if all are occupied:
int? SecondMoveDiagonalCounterStrategy(char player) { var opponentValue = GetOpponentValue(player); if ((_layout[2] == opponentValue && _layout[6] == opponentValue) || (_layout[0] == opponentValue && _layout[8] == opponentValue)) return new[] {2, 4, 6, 8}.FirstOrDefault(position => _layout[position - 1] == '\0'); return null; }
In theory, our new changes should cover the gaps in our initial blocking strategy, but we don’t actually have a test for the specific defect that was reported. Let’s create a test which describes the specific steps reported in the defect:
// http://github/mygroup/tic-tac-toe/issues/1 [TestClass] public class When_a_player_attempts_to_choose_positions_1_5_2_and_8 { [TestMethod] public void it_should_prevent_the_player_from_winning() { var game = new Game(); var result = (GameResult) (-1); game.GameComplete += (s, e) => result = e.Result; new[] {1, 4, 2, 8}.ToList().ForEach(position => { if (result == (GameResult)(-1)) Catch.Exception(() => game.ChoosePosition(position)); }); Assert.AreNotEqual(GameResult.PlayerWins, result); } }
In this test, we’re choosing each position in sequence until the result changes. Since it’s possible that the game may throw an exception due to our choosing a position already occupied, we’re issuing our ChoosePosition() call within a call to our Catch.Exception() helper.
The test name for this test is a bit more obscure than our previous ones since it describes more of the “how” than the “why”, but since the purpose of this test is to correct the behavior reported by a specific defect, it seems appropriate to name the test after the scenario it’s intended to address. To aid in its documentation, we’ve included a simple comment containing a link to the issue that was filed.
Let’s run the test:
When_a_player_attempts_to_choose_positions_1_5_2_and_8 Failed it_should_prevent_the_player_from_winning TestFirstExample.When_player_attempts_to_choose_positions_1_5_2_and_8.it_should_prevent_the_player_from_winning threw exception: System.InvalidOperationException: Sequence contains no matching element
It appears this scenario uncovered an issue we didn’t run into with our previous specifications. Researching the issue, the cause appears to be that the GameAdvisor is throwing an exception in the GetNextWinningMoveForPlayer() method when the First() method is called on an empty bestSlice collection. Let’s fix this:
int GetNextWinningMoveForPlayer(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => _layout[p - 1] == player)).First(); return bestSlice.FirstOrDefault(p => _layout[p - 1] == '\0'); }
Now, let’s run our tests again:
When_a_player_attempts_to_choose_positions_1_5_2_and_8 Failed it_should_prevent_the_player_from_winning TestFirstExample.When_player_attempts_to_choose_positions_1_5_2_and_8.it_should_prevent_the_player_from_winning threw exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
We got passed that exception, but now there’s another one. Further analysis reveals that an exception is being thrown from the Game’s SelectAPositionFor() method when a recommended position of zero is returned from the GameAdvisor. The Game class now only calls the GameAdvisor when there are positions left, so it shouldn’t be returning zero. Stepping through the execution of the GameAdvisor, it turns out that it stops recommending positions once it runs out of meaningful offensive and defensive strategies.
We could correct this within the context of our existing test, but this feels more like missing behavior than just a bug. Since we want our GameAdvisor to continue recommending positions until there are no positions left, let’s write a new test to explicitly specify this new behavior:
[TestClass] public class When_the_game_selects_a_position_where_no_winning_spaces_are_left { [TestMethod] public void it_should_choose_the_first_available_position() { var gameAdvisor = new GameAdvisor(); int selection = gameAdvisor.WithLayout("0XOOX\0XOX").SelectBestMoveForPlayer('X'); Assert.AreEqual(6, selection); } }
When_the_game_selects_a_position_when_no_winning_spaces_are_left Failed it_should_choose_the_first_available_position TestFirstExample.When_the_game_selects_a_position_where_no_winning_spaces_are_left.it_should_choose_the_first_available_position threw exception: System.InvalidOperationException: Sequence contains no elements
Let’s make the test fail for the right reason:
public int SelectBestMoveForPlayer(char player) { return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player) ?? 1; } int? GetNextWinningMoveForPlayer(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); int? nextPosition = null; if (availablePaths != null) { int[] bestSlice = availablePaths.OrderByDescending(path => path.Count(p => _layout[p - 1] == player)).FirstOrDefault(); if (bestSlice != null) nextPosition = bestSlice.FirstOrDefault(p => _layout[p - 1] == '\0'); } return nextPosition; }
When_the_game_selects_a_position_when_no_winning_spaces_are_left Failed it_should_choose_the_first_available_position Assert.AreEqual failed. Expected:<6>. Actual:<1>.
To make the test pass, we should be able to select the first empty position as the default strategy:
public int SelectBestMoveForPlayer(char player) { return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player) ?? Enumerable.Range(1, 9).First(position => _layout[position - 1] == '\0'); }
Let’s run all our tests again:
This passes our new test as well as our initial broken test. Now that we’ve make the test pass, let’s refactor.
As with our Game class, let’s substitute our uses of the null character with the Game’s EmptyValue constant:
public class GameAdvisor : IGameAdvisor { ... public int SelectBestMoveForPlayer(char player) { return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player) ?? Enumerable.Range(1, 9).First(position => _layout[position - 1] == Game.EmptyValue); } int? GetNextWinningMoveForPlayer(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); int? nextPosition = null; if (availablePaths != null) { int[] bestSlice = availablePaths.OrderByDescending(path => path.Count(p => _layout[p - 1] == player)).FirstOrDefault(); if (bestSlice != null) nextPosition = bestSlice.FirstOrDefault(p => _layout[p - 1] == Game.EmptyValue); } return nextPosition; } ... int? SecondMoveDiagonalCounterStrategy(char player) { char opponentValue = GetOpponentValue(player); if ((_layout[2] == opponentValue && _layout[6] == opponentValue) || (_layout[0] == opponentValue && _layout[8] == opponentValue)) return new[] {2, 4, 6, 8}.FirstOrDefault(position => _layout[position - 1] == Game.EmptyValue); return null; } int? SimpleBlockStrategy(char player) { char opponentValue = GetOpponentValue(player); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string(path.Select(p => _layout[p - 1]).ToArray()) .Count(c => c == opponentValue) == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath.First(position => _layout[position - 1] == Game.EmptyValue); } return null; } ... }
For this to compile, we’ll also need to make this value public:
public class Game { … public const char EmptyValue = char.MinValue; … }
Let’s move on to our next issue:
| Issue | Description | Owner |
|---|---|---|
| Defect | The game throws an InvalidOperationException when choosing positions 1, 2, 5, and 9. |
In testing the previous version of the game, the exception being thrown originated from the GameAdvisor’s GetNextWinningMoveForPlayer() position. Since we’ve modified this method, the new version may no longer throw this exception. Let’s write our test and see what happens:
// http://github/mygroup/tic-tac-toe/issues/2 [TestClass] public class When_a_player_attempts_to_choose_positions_1_2_5_9 { [TestMethod] public void it_should_not_throw_an_exception() { Exception exception = null; var game = new Game(); var result = (GameResult) (-1); game.GameComplete += (s, e) => result = e.Result; new[] {1, 2, 5, 9}.ToList().ForEach(position => { Exception ex = null; if (result == (GameResult) (-1)) ex = Catch.Exception(() => game.ChoosePosition(position)); if (ex is InvalidOperationException ) exception = ex; }); Assert.IsNotInstanceOfType(exception, typeof (InvalidOperationException)); } }
As suspected, this error seems to have already been addressed somewhere along the way. Let’s break the test to make sure it’s working:
public void ChoosePosition(int position) { throw new InvalidOperationException(); ... }
When_a_player_attempts_to_choose_positions_1_2_5_9 Failed it_should_not_throw_an_exception Assert.IsNotInstanceOfType failed. Wrong Type:. Actual type: .
Here’s our next defect:
| Issue | Description | Owner |
|---|---|---|
| Defect | The game makes a move after the player wins |
I seem to recall we ran into this issue while redesigning the game to raise events when a player wins. I suspect this issue no longer exists, but let’s write a test for this defect to confirm:
// http://github/mygroup/tic-tac-toe/issues/3 [TestClass] public class When_the_player_chooses_a_position_which_wins_the_game { [TestMethod] public void it_should_not_select_a_position_for_the_game() { var game = new Game(new GameAdvisorStub(new[] {4, 5, 9, 6})); Enumerable.Range(1, 3).ToList().ForEach(game.ChoosePosition); var lastGameChoice = game.GetLastChoiceBy(Player.Game); Assert.AreNotEqual(6, lastGameChoice); } }
It looks like this issue is no longer present. Let’s break the test to make sure our test is validating correctly:
public int GetLastChoiceBy(Player player) { return 6; // return _lastPositionDictionary[player]; }
When_the_player_chooses_a_position_which_wins_the_game Failed it_should_not_select_a_position_for_the_game Assert.AreNotEqual failed. Expected any value except:<6>. Actual:<6>.
Everything looks correct, so we’ll revert the change:
After thinking about this issue, it occurred to me that our game probably doesn’t handle the reverse case of a player making a move after the game has been won. This seems like an issue we should address, but to avoid adding anything unnecessarily I checked with the customer and the UI team to see what the expectations were for this scenario. The customer said they wanted the game to tell the user the game was already over in this case, so it was decided that we should raise an exception that could be caught by the UI team. Let’s go ahead and write out test for this case:
[TestClass] public class When_the_player_selects_a_position_after_a_player_has_won { [TestMethod] public void it_should_tell_the_player_the_game_is_over() { Exception exception = null; var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); new[] { 4, 5, 7}.ToList().ForEach(game.ChoosePosition); exception = Catch.Exception(() => game.ChoosePosition(9)); Assert.AreSame(typeof(GameOverException), exception.GetType()); } }
Here’s the exception we need to make the test compile:
public class GameOverException : Exception { }
Let’s run the test:
When_the_player_selects_a_position_after_a_player_has_won Failed it_should_tell_the_player_the_game_is_over Assert.AreSame failed.
Now let’s make it pass. Let’s move our existing someoneWon variable to a field and raise our new exception if someone won or if no positions are left at the entry of our method:
bool _someoneWon; public void ChoosePosition(int position) { if (_someoneWon || !PositionsAreLeft()) { throw new GameOverException(); } if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != EmptyValue) { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } _someoneWon = new Func[] { () => CheckPlayerStrategy(Player.Human, () => _layout[position - 1] = GetTokenFor(Player.Human)), () => CheckPlayerStrategy(Player.Game, () => SelectAPositionFor(Player.Game)) }.Any(winningPlay => winningPlay()); if (!(_someoneWon || PositionsAreLeft())) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.Draw)); } }
There doesn’t appear to be anything to refactor, so let’s move on. Here’s our next defect:
| Issue | Description | Owner |
|---|---|---|
| Defect | After letting the game win by choosing positions 4, 7, 8, and 6, choosing the last position of 3 throws an InvalidOperationException. |
// http://github/mygroup/tic-tac-toe/issues/5 [TestClass] public class When_a_player_attempts_to_choose_positions_4_7_8_6 { [TestMethod] public void it_should_not_throw_an_exception() { Exception exception = null; var game = new Game(); var result = (GameResult)(-1); game.GameComplete += (s, e) => result = e.Result; new[] { 4, 7, 8, 6 }.ToList().ForEach(position => { Exception ex = null; if (result == (GameResult)(-1)) ex = Catch.Exception(() => game.ChoosePosition(position)); if (ex is InvalidOperationException) exception = ex; }); Assert.IsNotInstanceOfType(exception, typeof(InvalidOperationException)); } }
As with the others, let’s make sure the test is working properly:
When_a_player_attempts_to_choose_positions_4_7_8_6 Failed it_should_not_throw_an_exception Assert.IsNotInstanceOfType failed. Wrong Type:. Actual type: .
We’re almost done! Here’s our last defect:
| Issue | Description | Owner |
|---|---|---|
| Defect | When trying to let the game win by choosing positions 1, 7, and 8, the game chose positions 4, 5, and 9 instead of completing the winning sequence 4, 5, 6. |
This isn’t really a bug so much as a missing feature. Rather than addressing this issue with a test describing this specific set of moves, let’s describe the missing behavior whose expectations are implied by this defect:
[TestClass] public class When_the_game_can_win_with_the_next_move { [TestMethod] public void it_should_select_the_winning_position() { var game = new Game(); var result = (GameResult)(-1); game.GameComplete += (s, e) => result = e.Result; new[] { 1, 7, 8 }.ToList().ForEach(game.ChoosePosition); Assert.AreEqual(GameResult.GameWins, result); } }
When_the_game_can_win_with_the_next_move Failed it_should_not_throw_an_exception Assert.AreEqual failed. Expected:<GameWins>. Actual:<-1>.
I think this can be corrected with a new defensive strategy, so let’s take the leap of skipping a fake implementation and go ahead and add the new behavior:
class PositionSelector : IPositionSelector { ... int? GetPositionThreateningPlayer(char player) { return new DefensiveStrategy[] { PathCompletionStrategy, SimpleBlockStrategy, FirstMoveCounterCenterStrategy, SecondMoveDiagonalCounterStrategy } .Select(strategy => strategy(player)).FirstOrDefault(p => p.HasValue); } int? PathCompletionStrategy(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); int[] winningPath = availablePaths .Where(path => new string(path.Select(p => _layout[p - 1]).ToArray()) .Count(value => value == player) == 2).FirstOrDefault(); if(winningPath != null) return winningPath.FirstOrDefault(p => _layout[p - 1] == Game.EmptyValue); return null; } ... }
We’ve used this approach in another strategy, so let’s factor out the duplication:
int? PathCompletionStrategy(char player) { int[] winningPath = GetWinningPathForPlayer(player); if (winningPath != null) return winningPath.FirstOrDefault(p => _layout[p - 1] == Game.EmptyValue); return null; } int? SimpleBlockStrategy(char player) { int[] threatingPath = GetWinningPathForPlayer(GetOpponentValue(player)); if (threatingPath != null) { return threatingPath.First(position => _layout[position - 1] == Game.EmptyValue); } return null; } int[] GetWinningPathForPlayer(char player) { List<int[]> availablePaths = GetAvailablePathsFor(player); return availablePaths .Where(path => new string(path.Select(p => _layout[p - 1]).ToArray()) .Count(value => value == player) == 2).FirstOrDefault(); }
I think we’re finished. As a final step, I’m going to ask the UI team if I can get an unofficial build with our new component integrated and do a little smoke testing before I close out our issues. We’ll discuss the outcome of this endeavor next time.
While I’ve enjoyed working with the ASP.Net MVC Framework, one thing I wish that it provided is the ability to create controller-less actions. Why you ask? Because I’d rather my controllers only have one reason to change. While this isn’t provided out-of-the-box with ASP.Net MVC, single action controllers can be facilitated pretty easy with ASP.Net Routing.
Let’s say that we have a single CustomerController that has several actions for adding and retrieving customers which we’d like to split up. We could simply move these actions to separate controllers and register routes for each action to maintain the existing URL structure, but this could pose a maintenance issue down the road if you end up with many small controllers. There are some clever things we could do with dependency injection to provide a pretty nice solution, but I’m going to show you a really simple way of achieving this without a lot of extra wiring.
First, create a new class which extends System.Web.Routing.Route. Next, override the GetRouteData() method and paste in the following code:
public override RouteData GetRouteData(HttpContextBase httpContext) { RouteData routeData = base.GetRouteData(httpContext); if (routeData != null) { var controller = routeData.Values["controller"] as string; var action = routeData.Values["action"] as string; string controllerAction = string.Format("{0}{1}", controller, action); routeData.Values["controller"] = controllerAction; routeData.Values["action"] = "Execute"; } return routeData; }
Next, define a new route using your new custom Route class. To match the registration API provided by the framework, you can use the following extension method:
public static class RouteCollectionExtensions { public static Route MapActionRoute(this RouteCollection routes, string name, string url, object defaults, object constraints) { var route = new ActionRoute(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; routes.Add(name, route); return route; } }
With this in place, the path “~/Customer/Get/1” will route to the following controller:
public class CustomerGetController : Controller { public ActionResult Execute(string id) { var customer = CustomerRepository.Get(id); return View("CustomerView", customer); } }
To see a full working example, I’ve provided a source-only NuGet package here. If you have the NuGet Command Line tool, open up a shell and run the following where you want the source:
>nuget install ActionControllerExampleSource
If you have NuGet.exe in your path, you should be able to just build the solution. Otherwise, you’ll need to edit the batch file in the script directory to set the NuGet path so it can grab a few dependencies it uses. Enjoy.
In part 7 of our series, we finished the initial implementation of our Tic-tac-toe component. After we finished, a team in charge of creating a host application was able to get everything integrated (though rumor has it that there was a bit of complaining) and the application made its way to the QA team for some acceptance testing. Surprisingly, there were several issues reported that got assigned to us. Here are the issues we’ve been assigned:
| Issue | Description | Owner |
|---|---|---|
| Defect | The player can always win by choosing positions 1, 5, 2, and 8. The game should prevent the player from winning. | |
| Defect | The game throws an InvalidOperationException when choosing positions 1, 2, 5, and 9 | QA Team |
| Defect | The game makes a move after the player wins | QA Team |
| Defect | After letting the game win by choosing positions 4, 7, 8, and 6, choosing the last position of 3 throws an InvalidOperationException | QA Team |
| Defect | When trying to let the game win by choosing positions 1, 7, and 8, the game chose positions 4, 5, and 9 instead of completing the winning sequence 4, 5, 6. | QA Team |
| New Feature | Add a method to the Game class for retrieving the last position selected by the game. | |
| New Feature | Please modify the ChoosePosition method to throw exceptions for errors rather than returning strings. Additionally, please provide an event we can subscribe to when one of the players wins or when there is a draw. | GUI Team |
As you may have discerned by now, following Test-Driven Development doesn’t ensure the code we produce will be free of errors. It does, however, ensure that our code meets the executable specifications we create to guide the application’s design and aids in the creation of code that’s maintainable and relevant (that is, to the extent we adhere to Test-Driven Development methodologies). Of course, the Test-Driven Development process is a framework into which we pour both our requirements and ourselves. The quality of both of these ingredients certainly affects the overall outcome. As we become better at gathering requirements, translating these requirements into executable specifications, identifying simple solutions and factoring out duplication, our yield from the TDD process will increase.
Since our issues have no particular priority assigned, let’s read through them before we get started to see if any make sense to tackle first. Given a couple of the issues pertain to changes to the API, it might be best to address these first to minimize the possibility of writing new tests that could need to be modified later.
The first of these issues pertain to adding a new method. Here’s the issue:| Issue | Description | Owner |
|---|---|---|
| Add a method to the Game class for retrieving the last position selected by the game. |
Upon integrating our component, the GUI team discovered there wasn’t an easy way to tell what positions the game was selecting in order to reflect this to the user. They were able to use techniques similar to those we used in the course of implementing our tests, but they didn’t consider this to be a very friendly API. While such an oversight may seem obvious within the context of the entire application, such issues occur when components are development in isolation. Fundamentally, the problem wasn’t with the Test-Driven Development methodologies we were following, but with the scope in which we were applying them. Later in our series, we’ll discuss an alternative to the approach we’ve taken with this effort that can help avoid misalignments such as this. Until then, we’ll address these issues the best we can with our existing approach.
To address this issue, we’ll create a new test that describes the behavior for the requested method:
[TestClass] public class When_retrieving_the_last_selected_position_for_the_game { [TestMethod] public void it_should_return_the_last_position() { } }
As our method of validation, we’ll set up our assertion to verify that some expected position was selected:
[TestClass] public class When_retrieving_the_last_selected_position_for_the_game { [TestMethod] public void it_should_return_the_last_position() { Assert.AreEqual(1, selection); } }
Next, let’s choose what our API is going to look like and then set up the context of our test. Let’s call our new method GetLastChoiceBy(). We can make use of our existing Player enumeration as the parameter type:
[TestClass] public class When_retrieving_the_last_selected_position_for_the_game { [TestMethod] public void it_should_return_the_last_position() { Game game = new Game(new GameAdvisorStub(new [] { 1 })); game.GoFirst(); var selection = game.GetLastChoiceBy(Player.Game); Assert.AreEqual(1, selection); } }
Next, let’s add the new method to our Game class so this will compile:
public class Game { // snip public int GetLastChoiceBy(Player player) { return 0; } }
Now we’re ready to run the tests:
When_retrieving_the_last_selected_position_for_the_game Failed it_should_return_the_last_position Assert.AreEqual failed. Expected:<1>. Actual:<0>.
We can make the test pass by just returning a 1:
public int GetLastChoiceBy(Player player) { return 1; }
Now, we’ll refactor the method to retrieve the value from a new dictionary field which we’ll set in the SelectAPositionFor() method:
public class Game { readonly Dictionary<Player, char> _tokenAssignments = new Dictionary<Player, char>(); ... void SelectAPositionFor(Player player) { int recommendedPosition = _advisor.WithLayout(new string(_layout)).SelectBestMoveForPlayer(GetTokenFor(player)); _layout[recommendedPosition - 1] = GetTokenFor(player); _lastPositionDictionary[player] = recommendedPosition; } public int GetLastChoiceBy(Player player) { return _lastPositionDictionary[player]; } }
That was fairly simple. On to our next feature request:
| Issue | Description | Owner |
|---|---|---|
| New Feature | Please modify the ChoosePosition method to throw exceptions for errors rather than returning strings. Additionally, please provide an event we can subscribe to when one of the players wins or when there is a draw. | GUI Team |
This is slightly embarrassing. While we’ve been striving to guide the design of our public interface from a consumer’s perspective, we seem to have made a poor choice in how the game communicates errors and the concluding status of the game. If our Game class had evolved within the context of the consuming application, perhaps we would have seen these choices in a different light.
Let’s go ahead and get started on the first part of the request which involves changing how errors are reported. First, let’s take inventory of our existing tests which relate to reporting errors. We have two tests which pertain to error conditions:
[TestClass] public class When_the_player_attempts_to_select_an_invalid_position { [TestMethod] public void it_should_tell_the_player_the_position_is_invalid() { var game = new Game(); string message = game.ChoosePosition(10); Assert.AreEqual("That spot is invalid!", message); } }
[TestClass] public class When_the_player_attempts_to_select_an_occupied_position { [TestMethod] public void it_should_tell_the_player_the_position_is_occupied() { var game = new Game(new GameAdvisorStub(new[] { 1, 4, 7 })); game.ChoosePosition(2); string message = game.ChoosePosition(1); Assert.AreEqual("That spot is taken!", message); } }
Starting with the first method, let’s modify it to check that an exception was thrown:
[TestClass] public class When_the_player_attempts_to_select_an_invalid_position { [TestMethod] public void it_should_tell_the_player_the_position_is_invalid() { var game = new Game(); string message = game.ChoosePosition(10); Assert.AreEqual("The position '10' was invalid.", exception.Message); } }
Next, let’s wrap the call to the ChoosePosition() method with a try/catch block. We’ll call our exception an InvalidPositionException:
[TestClass] public class When_the_player_attempts_to_select_an_invalid_position { [TestMethod] public void it_should_tell_the_player_the_position_is_invalid() { var exception = new InvalidPositionException(string.Empty); var game = new Game(); try { game.ChoosePosition(10); } catch (InvalidPositionException ex) { exception = ex; } Assert.AreEqual("The position '10' was invalid.", exception.Message); } }
Next, let’s create our new Exception class:
Now, let’s run our tests:public class InvalidPositionException : Exception { public InvalidPositionException(string message) : base(message) { } }
When_the_player_attempts_to_select_an_invalid_position Failed it_should_tell_the_player_the_position_is_invalid Assert.AreEqual failed. Expected:. Actual:<>.
Since all we need to do is to throw our new exception, we’ll use an Obvious Implementation:
public string ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException( string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { return "That spot is taken!"; } _layout[position - 1] = GetTokenFor(Player.Human); SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Human)) return "Player wins!"; if (WinningPlayerIs(Player.Game)) return "Game wins."; return string.Empty; }
In this case we haven’t introduced any duplication that I can see, so let’s move on to our next test. We’ll modify it to follow the same pattern as our previous one:
[TestClass] public class When_the_player_attempts_to_select_an_occupied_position { [TestMethod] public void it_should_tell_the_player_the_position_is_occupied() { var exception = new OccupiedPositionException(string.Empty); var game = new Game(new GameAdvisorStub(new[] {1, 4, 7})); game.ChoosePosition(2); try { game.ChoosePosition(1); } catch (OccupiedPositionException ex) { exception = ex; } Assert.AreEqual("The position '1' is already occupied.", exception.Message); } }
Here is our new exception:
public class OccupiedPositionException : Exception { public OccupiedPositionException(string message): base(message) { } }
Here’s the results of our test execution:
When_the_player_attempts_to_select_an_occupied_position Failed it_should_tell_the_player_the_position_is_occupied Assert.AreEqual failed. Expected:. Actual:<>.
To provide the implementation, we’ll throw our new exception:
public string ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException( string.Format("The position \'{0}\' is already occupied.", position)); } _layout[position - 1] = GetTokenFor(Player.Human); SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Human)) return "Player wins!"; if (WinningPlayerIs(Player.Game)) return "Game wins."; return string.Empty; }
Again, there isn’t any noticeable duplication this time. Our next task is to send notifications to observers when the Game class detects a winner. Here are the existing tests we used for indicating a winner:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); game.ChoosePosition(4); game.ChoosePosition(5); string message = game.ChoosePosition(6); Assert.AreEqual("Player wins!", message); } }
[TestClass] public class When_the_game_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_game_as_the_winner() { var game = new Game(); game.ChoosePosition(4); game.ChoosePosition(6); string message = game.ChoosePosition(8); Assert.AreEqual("Game wins.", message); } }
We’ll start with the first one by modifying our Assert call. First, let’s change the value we’re comparing against from a string to a new GameResult enumeration with a value of PlayerWins:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); game.ChoosePosition(4); game.ChoosePosition(5); string message = game.ChoosePosition(6); Assert.AreEqual(GameResult.PlayerWins, result); } }
Next, let’s create an instance of our as yet created GameResult enumeration and initialize it’s value to something we aren’t expecting:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); var result = (GameResult) (-1); game.ChoosePosition(4); game.ChoosePosition(5); string message = game.ChoosePosition(6); Assert.AreEqual(GameResult.PlayerWins, result); } }
Next, we need to decide how we would like to receive this value. Let’s assume we can subscribe to a GameComplete event. When invoked, we’ll assume the value can be retrieved from a property on the EventArgs supplied with the event:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); var result = (GameResult) (-1); game.GameComplete += (s, e) => result = e.Result; game.ChoosePosition(4); game.ChoosePosition(5); game.ChoosePosition(6); Assert.AreEqual(GameResult.PlayerWins, result); } }
Our next steps are to create the new enum type and to add an event to our Game class. First, let’s create the enum:
public enum GameResult { PlayerWins, GameWins, Draw }
I went ahead and added values for the other two possible states: GameWins and Draw. “Aren’t we getting ahead of ourselves”, you might ask? Perhaps, but we already know we have upcoming tests that will require these states and our GameResult represents the state of our game, not its behavior. We’ve been pretty good about not prematurely adding anything thus far, so this seems like a safe enough step to take without sending us down a slippery slope.
Here’s our new Game event:
public class Game { ... public event EventHandlerGameComplete; ... }
Now that we’ve created this, we’ll also need to create a GameCompleteEventArgs:
public class GameCompleteEventArgs : EventArgs { public GameResult Result { get; private set; } }
Now we’re ready to compile and run our tests:
When_the_player_gets_three_in_a_row Failed it_should_announce_the_player_as_the_winner Assert.AreEqual failed. Expected:. Actual:<-1>.
There are well established patterns for raising events in .Net, so we’ll follow the standard pattern for this:
public string ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } _layout[position - 1] = GetTokenFor(Player.Human); SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Human)) InvokeGameComplete(new GameCompleteEventArgs(GameResult.PlayerWins)); if (WinningPlayerIs(Player.Game)) return "Game wins."; return string.Empty; }
Now we need to create our InvokeGameComplete() method and a GameCompleteEventArgs constructor that initializes the Result property:
public class Game { ... public event EventHandlerGameComplete; public void InvokeGameComplete(GameCompleteEventArgs e) { EventHandler handler = GameComplete; if (handler != null) handler(this, e); } ... }
public class GameCompleteEventArgs : EventArgs { public GameCompleteEventArgs(GameResult result) { Result = result; } public GameResult Result { get; private set; } }
Again, I don’t see any duplication to worry about. Next, we’ll follow similar steps for notifying the game as a winner:
[TestClass] public class When_the_game_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_game_as_the_winner() { var game = new Game(new GameAdvisorStub(new[] {1, 2, 3})); var result = (GameResult) (-1); game.GameComplete += (s, e) => result = e.Result; game.ChoosePosition(4); game.ChoosePosition(6); game.ChoosePosition(8); Assert.AreEqual(GameResult.GameWins, result); } }
When_the_game_gets_three_in_a_row Failed it_should_announce_the_game_as_the_winner Assert.AreEqual failed. Expected:. Actual:<-1>.
To make the test pass, we should only need to modify the Game class to raise the event this time:
public string ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } _layout[position - 1] = GetTokenFor(Player.Human); SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Human)) InvokeGameComplete(new GameCompleteEventArgs(GameResult.PlayerWins)); if (WinningPlayerIs(Player.Game)) InvokeGameComplete(new GameCompleteEventArgs(GameResult.GameWins)); return string.Empty; }
Let’s run the tests again:
When_the_player_gets_three_in_a_row Failed it_should_announce_the_player_as_the_winner Assert.AreEqual failed. Expected:. Actual: .
Our target test passed, but we broke our previous test. Looking at our implementation, the problem seems to be that both the player and game select positions on the board before we check to see if anyone is a winner. Additionally, we should return from the method once a winner is determined. Let’s fix this:
public string ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } _layout[position - 1] = GetTokenFor(Player.Human); if (WinningPlayerIs(Player.Human)) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.PlayerWins)); return string.Empty; } SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Game)) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.GameWins)); return string.Empty; } return string.Empty; }
Let’s refactor now. First, let’s remove our unused return type:
public void ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } _layout[position - 1] = GetTokenFor(Player.Human); if (WinningPlayerIs(Player.Human)) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.PlayerWins)); return; } SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Game)) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.GameWins)); return; } }
Now that we’ve rearranged our code, we have a sequence of steps that are repeated between the player and the game. First we use a strategy for moving the player, then we check to see if the player wins. Let’s distill this down to checking for the first winning play from a collection of player strategies:
public void ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } new Func<bool>[] { () => CheckPlayerStrategy(Player.Human, () => _layout[position - 1] = GetTokenFor(Player.Human)), () => CheckPlayerStrategy(Player.Game, () => SelectAPositionFor(Player.Game)) }.Any(winningPlay => winningPlay()); }
Here’s our new CheckPlayerStrategy() method:
bool CheckPlayerStrategy(Player player, Action strategy) { strategy(); if (WinningPlayerIs(player)) { var result = (player == Player.Human) ? GameResult.PlayerWins : GameResult.GameWins; InvokeGameComplete(new GameCompleteEventArgs(result)); return true; } return false; }
Our final step for this issue is to raise an event when there is a draw. Following our normal procession, here’s the test we come up with:
[TestClass] public class When_a_move_results_in_a_draw { [TestMethod] public void it_should_announce_the_game_is_a_draw() { var game = new Game(new GameAdvisorStub(new[] { 2, 3, 4, 9 })); var result = (GameResult)(-1); game.GameComplete += (s, e) => result = e.Result; new[] {1, 5, 6, 7, 8}.ToList().ForEach(game.ChoosePosition); Assert.AreEqual(GameResult.Draw, result); } }
When_a_move_results_in_a_draw Failed it_should_announce_the_game_is_a_draw TestFirstExample.When_a_move_results_in_a_draw.it_should_announce_the_game_is_a_draw threw exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
This test isn’t failing for the right reason, so let’s address this before moving on. After investigating the exception, the issue is that we never accounted for the fact that their won’t be a position to choose when the player chooses the last remaining position. Let’s correct this issue by ensuring there is an empty spot left before selecting a position for the game:
void SelectAPositionFor(Player player) { if (_layout.Any(position => position == '\0')) { int recommendedPosition = _advisor.WithLayout(new string(_layout)).SelectBestMoveForPlayer(GetTokenFor(player)); _layout[recommendedPosition - 1] = GetTokenFor(player); _lastPositionDictionary[player] = recommendedPosition; } }
When_a_move_results_in_a_draw Failed it_should_announce_the_game_is_a_draw Failed it_should_announce_the_game_is_a_draw Assert.AreEqual failed. Expected:. Actual:<-1>.
Now our test is failing for the right reason. To make the test pass, we can fire the event unless someone won or unless there’s any empty positions left:
public void ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } var someoneWon = new Func<bool>[] { () => CheckPlayerStrategy(Player.Human, () => _layout[position - 1] = GetTokenFor(Player.Human)), () => CheckPlayerStrategy(Player.Game, () => SelectAPositionFor(Player.Game)) }.Any(winningPlay => winningPlay()); if (!(someoneWon || _layout.Any(pos => pos == '\0'))) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.Draw)); } }
Time to refactor. It looks like we have the same comparison for checking that the game is a draw and our new guard for selecting a position for the game. Let’s create a method for these which expresses the meaning of this check:
bool PositionsAreLeft() { return _layout.Any(pos => pos == '\0'); }
Now we can replace the previous calls in the ChoosePosition() and SelectAPositionFor() methods:
public void ChoosePosition(int position) { if (IsOutOfRange(position)) { throw new InvalidPositionException(string.Format("The position \'{0}\' was invalid.", position)); } if (_layout[position - 1] != '\0') { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } bool someoneWon = new Func<[] { () => CheckPlayerStrategy(Player.Human, () => _layout[position - 1] = GetTokenFor(Player.Human)), () => CheckPlayerStrategy(Player.Game, () => SelectAPositionFor(Player.Game)) }.Any(winningPlay => winningPlay()); if (!(someoneWon || PositionsAreLeft())) { InvokeGameComplete(new GameCompleteEventArgs(GameResult.Draw)); } }
void SelectAPositionFor(Player player) { if (PositionsAreLeft()) { int recommendedPosition = _advisor.WithLayout(new string(_layout)).SelectBestMoveForPlayer(GetTokenFor(player)); _layout[recommendedPosition - 1] = GetTokenFor(player); _lastPositionDictionary[player] = recommendedPosition; } }
One thing that occurred to me while implementing this feature is that using a null character to represent an empty position isn’t particularly clear. Let’s define a constant named EmptyValue which we’ll substitute for our use of the null character:
public class Game { ... const char EmptyValue = char.MinValue; ... public void ChoosePosition(int position) { ... if (_layout[position - 1] != EmptyValue) { throw new OccupiedPositionException(string.Format("The position \'{0}\' is already occupied.", position)); } ... } bool PositionsAreLeft() { return _layout.Any(pos => pos == EmptyValue ); } string GetLayoutFor(Player player) { return new string(_layout.ToList() .Select(c => (c.Equals(GetTokenFor(player))) ? GetTokenFor(player) : EmptyValue ) .ToArray()); } … }
That wraps up the two issues from the UI team. We’ll stop here and address the issues from the QA team next time.
In part 6 of our series, we continued the implementation of our Tic-tac-toe game using a Test-First approach. This time, we’ll finish out our requirements.
Here’s where we left things:
When the player goes first it should put their mark in the selected position it should make the next moveWhen the player gets three in a row it should announce the player as the winner When the game gets three in a row it should announce the game as the winner When the player attempts to select an occupied position it should tell the player the position is occupied When the player attempts to select an invalid position it should tell the player the position is invalid When the game goes first it should put an X in one of the available positionsWhen the player can not win on the next turn it should try to get three in a row When the player can win on the next turn it should block the player
Our last two requirements pertain to making the game try to win. The first requirement concerns the game trying to get three in a row while the second pertains to the game trying to keep the opponent from getting three in a row. Let’s get started on the first test:
[TestClass] public class When_the_player_can_not_win_on_the_next_turn { [TestMethod] public void it_should_try_to_get_three_in_a_row() { } }
Let’s assume we’ll be validating that the game gets three in a row by completing a sequence ending with the bottom right position being selected:
[TestMethod] public void it_should_try_to_get_three_in_a_row() { Assert.AreEqual(9, selection); }
Next, let’s establish a scenario were the bottom right corner should always be the position we would expect the game to choose (as opposed to a scenario where the game might have multiple intelligent moves). The following illustrates a layout where the game has gone first and has already achieved two in a row:
Next, we need to determine how we can force the game into the desired state so we can validate the next position selected. We won’t be able to use the same technique as before, so we’ll need to find a new way of manipulating the state. One way would be to just make the Game’s _layout field public and manipulate it directly, but that would break encapsulation. Another way would be to set the _layout field through reflection, but this would result in our test being more tightly coupled to the implementation details of our Game. To make our Game testable, we need to adapt its interface. If our game relied upon a separate class for choosing the positions, we would then have a seam we could use to influence the layout. Hey … once this is in place we’ll have a way to fix our test coupling problem!
For now, let’s comment out the test we’ve been working on and start on a new test describing how the Game class will interact with this new dependency. Let’s think of the dependency as an “advisor” and describe the interaction as receiving a recommendation by the advisor:
[TestClass] public class When_the_game_selects_a_position { [TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { } }
Next, let’s establish an assertion that validates the selection made by the game. We’ll stick with the same scenario we established earlier, expecting the game to choose the bottom right position:
[TestClass] public class When_the_game_selects_a_position { [TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { Assert.AreEqual(9, selection); } }
Next, we need a way of determining the last position chosen by the game. As we’ve done in our previous tests, we’ll use the single GetPosition() method and a little bit of LINQ goodness to help us out. To figure out what the last move was, we can get a list of all the game positions before and after its next turn. We can then use the two lists to determine which new position was selected:
[TestClass] public class When_the_game_selects_a_position { [TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { IEnumerable<int> beforeLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); // make move here IEnumerable<int> afterLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); int selection = afterLayout.Except(beforeLayout).Single(); Assert.AreEqual(9, selection); } }
Next, let’s establish the Game context along with the call we’re interested in:
[TestClass] public class When_the_game_selects_a_position { [TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { var game = new Game(advisor); game.GoFirst(); game.ChoosePosition(1); IEnumerable<int> beforeLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); game.ChoosePosition(8); IEnumerable<int> afterLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); int selection = afterLayout.Except(beforeLayout).Single(); Assert.AreEqual(9, selection); } }
Next, let’s establish our GameAdvisor stub. To get our GameAdvisorStub to recommend the positions we’d like, we’ll pass an array of integers to denote the progression we want the game to use:
[TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { IGameAdvisor advisor = new GameAdvisorStub(new[] { 3, 6, 9 }); var game = new Game(advisor); game.GoFirst(); game.ChoosePosition(1); IEnumerable<int> beforeLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); game.ChoosePosition(8); IEnumerable<int> afterLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); int selection = afterLayout.Except(beforeLayout).Single(); Assert.AreEqual(9, selection); }
To get our test to compile, we’ll need to create our new IGameAdvisor interface, GameAdvisorStub class and add a constructor to our existing Game class. Let’s start with the advisor types:
Next, let’s create the new constructor for our Game class which takes an IGameAdvisor. We’ll also supply a default no argument constructor to keep our existing tests compiling:public interface IGameAdvisor { } public class GameAdvisorStub : IGameAdvisor { readonly int[] _positions; public GameAdvisorStub(int[] positions) { _positions = positions; } }
public class Game { public Game() { } public Game(IGameAdvisor advisor) { } ... }
Everything should now compile. Let’s run our tests:
When_the_game_selects_a_position Failed it_should_select_the_position_recommended_by_the_advisor Assert.AreEqual failed. Expected:<9>. Actual:<2>. ...
Before we move on, our test could stand a little cleaning up. The verbosity of the LINQ extension method calls we’re using are obscuring the intent of our test a bit. Let’s write a test helper in the form of an extension method to help clarify the intentions of our test:
public static class GameExtensions { public static int GetSelectionAfter(this Game game, Action action) { IEnumerable<int> beforeLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); action(); IEnumerable<int> afterLayout = (Enumerable.Range(1, 9) .Where(position => game.GetPosition(position).Equals('X')) .Select(position => position)).ToList(); return afterLayout.Except(beforeLayout).Single(); } }
Now we can change our test to the following:
[TestClass] public class When_the_game_selects_a_position { [TestMethod] public void it_should_select_the_position_recommended_by_the_advisor() { IGameAdvisor advisor = new GameAdvisorStub(new[] {3, 6, 9}); var game = new Game(advisor); game.GoFirst(); game.ChoosePosition(1); int selection = game.GetSelectionAfter(() => game.ChoosePosition(8)); Assert.AreEqual(9, selection); } }
Let’s run the test again to make sure it still validates correctly:
When_the_game_selects_a_position Failed it_should_select_the_position_recommended_by_the_advisor Assert.AreEqual failed. Expected:<9>. Actual:<2>. ...
Good, now let’s work on making the test pass. Something simple we can do to force our test to pass is to play off of a bit of new information we have at our disposal. Since our new test is the only one using the overloaded constructor, we can use the advisor field as a sort of flag to perform some behavior specific to this test. First, let’s assign the parameter to a field:
readonly IGameAdvisor _advisor; public Game(IGameAdvisor advisor) { _advisor = advisor; }
Next, let’s modify the ChoosePosition() method to set the ninth position to an ‘X’ if the _advisor field is set and the player chooses position 8:
public string ChoosePosition(int position) { if( _advisor != null && position == 8 ) { _layout[8] = 'X'; return string.Empty; } if (IsOutOfRange(position)) { return "That spot is invalid!"; } if (_layout[position - 1] != '\0') { return "That spot is taken!"; } _layout[position - 1] = GetTokenFor(Player.Human); SelectAPositionFor(Player.Game); if (WinningPlayerIs(Player.Human)) return "Player wins!"; if (WinningPlayerIs(Player.Game)) return "Game wins."; return string.Empty; }
Now, let’s run our tests:
Now, let’s refactor. To eliminate our fake implementation, let’s start by modifying the Game’s SelectAPositionFor() method to call our new IGameAdvisor field. Well assume the IGameAdvisor supports a SelectAPositionFor() method which allows us to pass in the token and the current layout as a string:
void SelectAPositionFor(Player player) { int recommendedPosition = _advisor.SelectAPositionFor(GetTokenFor(player), new string(_layout)); _layout[recommendedPosition] = GetTokenFor(player); }
Next, let’s define the new method on our interface:
public interface IGameAdvisor { int SelectAPositionFor(char player, string layout); }
Next, we need to implement the new method on our stub. To have our stub return the expected positions, we’ll keep track of how many times the method has been called and use that as the offset into the array setup in our test:
public class GameAdvisorStub : IGameAdvisor { readonly int[] _positions; int _count; public GameAdvisorStub(int[] positions) { _positions = positions; } public int SelectAPositionFor(char player, string layout) { return _positions[_count++]; } }
Lastly, we can delete our fake implementation and run the tests:
When_the_player_goes_first Failed it_should_put_their_choice_in_the_selected_position TestFirstExample.When_the_player_goes_first.establish_context threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object.. Failed it_should_make_the_next_move TestFirstExample.When_the_player_goes_first.establish_context threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object.. ...
Oh no, we broke a bunch of tests! They all seem to be failing due to a NullReferenceException. Looking further, this is being caused by the IGameAdvisor field not being assigned when using the default constructor. Let’s fix that by changing our default constructor to call the overloaded constructor with a default implementation of the IGameAdvisor interface:
public Game() : this(new GameAdvisor()) { }
Next, we’ll create the GameAdvisor class and provide an implementation that mirrors the former behavior:
class GameAdvisor : IGameAdvisor { public int SelectAPositionFor(char player, string layout) { return Enumerable.Range(1, layout.Length) .First(p => layout[p - 1].Equals('\0')); } }
Our Game class works the same way as before, but we now have a new seam we can influence the layout selection with.
We can now turn our attention back to the requirements. Let’s go back and un-comment the test we started with, but this time we’ll use it to drive the behavior of our GameAdvisor:
[TestClass] public class When_the_player_can_not_win_on_the_next_turn { [TestMethod] public void it_should_try_to_get_three_in_a_row() { Assert.AreEqual(9, selection); } }
Next, let’s declare an instance of our GameAdvisor class and ask it to select a position for player ‘X’:
[TestClass] public class When_the_player_can_not_win_on_the_next_turn { [TestMethod] public void it_should_try_to_get_three_in_a_row() { IGameAdvisor advisor = new GameAdvisor(); var selection = advisor.SelectAPositionFor('X', "OXXO"); Assert.AreEqual(9, selection); } }
Before moving on, let’s consider our initial API. Given any approach, is this really the API we want to work with? While the SelectAPositionFor() method seems like a good start, the parameters feel more like an afterthought than a part of the request. It reads more like a “Do something, and oh by the way, here’s some data”. I didn’t notice when we called it from the Game class, but looking back, that call was aided by the context of its usage. We don’t have any variables telling us what ‘X’ and “OA…” mean.
One of the advantages of Test-Driven Development is that it forces us to look at our API from a consumer’s perspective. When we build things from the inside out, we often don’t consider how intuitive the components will be to work with by our consumers. Once we’ve started implementation, our perspective can be prejudiced by our understanding of how the system works. TDD helps to address this issue by forcing us to consider how the components will be used. This in turn guides us to adapt the design to how the system is being used rather than the other way around. Let’s see if we can improve upon this a bit:
[TestClass] public class When_the_player_can_not_win_on_the_next_turn { [TestMethod] public void it_should_try_to_get_three_in_a_row() { IGameAdvisor advisor = new GameAdvisor(); var selection = advisor.WithLayout("OXXO").SelectBestMoveForPlayer('X'); Assert.AreEqual(9, selection); } }
That seems to express how I’d like to interact with our advisor more clearly. This breaks our code though, so we’ll need to make some adjustments to our IGameAdvisor interface and GameAdvisor class:
public interface IGameAdvisor { int SelectBestMoveForPlayer(char player); IGameAdvisor WithLayout(string layout); } class GameAdvisor : IGameAdvisor { string _layout; public int SelectBestMoveForPlayer(char player) { return Enumerable.Range(1, _layout.Length) .First(p => _layout[p - 1].Equals('\0')); } public IGameAdvisor WithLayout(string layout) { _layout = layout; return this; } }
That would work, but this implementation would allow us to call the advisor without specifying the layout. Let’s take just a little more time to clear that up by moving the SelectBestMoveForPlayer() method to an inner class to prevent it from being called directly:
public interface IGameAdvisor { IPositionSelector WithLayout(string layout); } public interface IPositionSelector { int SelectBestMoveForPlayer(char player); } class GameAdvisor : IGameAdvisor { public IPositionSelector WithLayout(string layout) { return new PositionSelector(layout); } class PositionSelector : IPositionSelector { readonly string _layout; public PositionSelector(string layout) { _layout = layout; } public int SelectBestMoveForPlayer(char player) { return Enumerable.Range(1, _layout.Length) .First(p => _layout[p - 1].Equals('\0')); } } }
Next, let’s fix up our GameAdvisorStub:
public class GameAdvisorStub : IGameAdvisor { readonly int[] _positions; int _count; public GameAdvisorStub(int[] positions) { _positions = positions; } public IPositionSelector WithLayout(string layout) { return new PositionSelector(layout, _positions, _count++); } class PositionSelector : IPositionSelector { readonly int[] _positions; readonly int _count; public PositionSelector(string layout, int[] positions, int count) { _positions = positions; _count = count; } public int SelectBestMoveForPlayer(char player) { return _positions[_count]; } } }
We also need to fix the SelectAPositionFor() method in our Game class:
void SelectAPositionFor(Player player) { int recommendedPosition = _advisor.WithLayout(new string(_layout)) .SelectBestMoveForPlayer(GetTokenFor(player)); _layout[recommendedPosition - 1] = GetTokenFor(player); }
Now, let’s run our tests and make sure our new test fails for the right reason and that we haven’t broken any of the other behavior:
When_the_player_can_not_win_on_the_next_turn Failed it_should_try_to_get_three_in_a_row Assert.AreEqual failed. Expected:<9>. Actual:<2>.
Only our new test fails, which is what we were hoping for. Now, let’s use the Fake It approach to get our test to pass quickly. Since only one of our tests ever call this method with the token ‘X’ and it doesn’t care about which actual position it is, we can change the GameAdvisor’s PositionAdvisor.SelectBestMoveForPlayer() method to always return 9 for player ‘X’:
public int SelectBestMoveForPlayer(char player) { if (player == 'X') return 9; return Enumerable.Range(0, _layout.Length) .First(p => _layout[p].Equals('\0')) + 1; }
Now, let’s refactor to eliminate our duplication. In order to calculate which position should be selected, we’ll need to know what the winning patterns are and which paths in the layout are closest to the winning patterns. My first thought was that we might be able to reuse the winning pattern regular expressions we defined over in our Game class. Let’s go back and look at that again:
readonly string[] _winningPatterns = new[] { "[XO][XO][XO]......", "...[XO][XO][XO]...", "......[XO][XO][XO]", "[XO]..[XO]..[XO]..", ".[XO]..[XO]..[XO].", "..[XO]..[XO]..[XO]", "[XO]...[XO]...[XO]", "..[XO].[XO].[XO]..", };
While these patterns define what the winning paths are, I don’t think this will work for our needs because this only matches winning patterns. What we need is a way of examining each of the eight winning paths within the layout to see which is the closest to winning. Let’s start by define a new regular expression that we can use to filter out the paths that can’t win:
class PositionSelector : IPositionSelector { readonly Regex _availablePathPattern = new Regex(@"[X]{3}"); readonly string _layout; public PositionSelector(string layout) { _layout = layout; } public int SelectBestMoveForPlayer(char player) { if (player == 'X') return 9; return Enumerable.Range(0, _layout.Length) .First(p => _layout[p].Equals('\0')) + 1; } }
This regular expression will match any three characters where each of the characters can be either an ‘X’ or a null. If you’re unfamiliar with regular expressions, the brackets are referred to as Character Classes or Character Sets and allow us to define a group of characters we’re interested in. The curly braces with the number is how we define how many times the pattern should repeat to be a match.
Now, we need to apply this to each of the eight possible paths within our layout. To do so, we’ll need to slice up our layout into the eight possible winning paths. Let’s define an array similar to the one in our Game class, but using the winning positions instead of winning patterns:
class PositionSelector : IPositionSelector { readonly Regex _availablePathPattern = new Regex(@"[X]{3}"); readonly string _layout; static readonly int[][] _winningPositions = new[] { new[] {1, 2, 3}, new[] {4, 5, 6}, new[] {7, 8, 9}, new[] {1, 4, 7}, new[] {2, 5, 8}, new[] {3, 6, 9}, new[] {1, 5, 9}, new[] {3, 5, 7}, }; public PositionSelector(string layout) { _layout = layout; } public int SelectBestMoveForPlayer(char player) { if (player == 'X') return 9; return Enumerable.Range(0, _layout.Length) .First(p => _layout[p].Equals('\0')) + 1; } }
Next, we can loop over each of the _winningPositions, retrieve the slice, compare it to the _availablePathPattern and add the matches to a list of availablePaths:
public int SelectBestMoveForPlayer(char player) { var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => _layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } if (player == 'X') return 9; return Enumerable.Range(0, _layout.Length) .First(p => _layout[p].Equals('\0')) + 1; }
Now that we have the available paths, we can sort them in descending order based on how many ‘O’s they already have, find the first available slot in the slice and return that as the position:
public int SelectBestMoveForPlayer(char player) { var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => _layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } var bestSlice = availablePaths .OrderByDescending(path => path .Count(p => _layout[p - 1] == 'X')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
I think we’re almost done refactoring, but we still have some duplication to eliminate. Both our test and our implementation are using the value of ‘X’ as a constant to represent the player. Let’s fix this by replacing the player’s token to a more neutral value and change our regular expression and sorting call to use the neutral value instead:
readonly Regex _availablePathPattern = new Regex(@"[T]{3}"); public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } int[] bestSlice = availablePaths .OrderByDescending(path => path .Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Everything should be good to go. Let’s run our tests and see how we did:
When_the_player_attempts_to_select_an_occupied_positionFailed it_should_tell_the_player_the_position_is_occupied Assert.AreEqual failed. Expected:<That spot is taken!>. Actual:<>.
Our target test passed, but we broke the test for how the game responds when the player chooses a position that is already occupied. Let’s review the test again:
[TestClass] public class When_the_player_attempts_to_select_an_occupied_position { [TestMethod] public void it_should_tell_the_player_the_position_is_occupied() { var game = new Game(); game.ChoosePosition(2); string message = game.ChoosePosition(1); Assert.AreEqual("That spot is taken!", message); } }
Because we are choosing the second position in this test, the GameAdvisor avoids recommending positions within the first winning pattern. We could fix this test pretty easily by avoiding positions two or three, but now that we now have a seam to control exactly what positions are selected, let’s use our new GameAdvisorStub to correct this test in a more explicit way:
[TestClass] public class When_the_player_attempts_to_select_an_occupied_position { [TestMethod] public void it_should_tell_the_player_the_position_is_occupied() { var game = new Game(new GameAdvisorStub(new [] {1, 4, 7})); game.ChoosePosition(2); string message = game.ChoosePosition(1); Assert.AreEqual("That spot is taken!", message); } }
The last requirement concerns how the game reacts when the player is about to win. Here’s our skeleton:
[TestClass] public class When_the_player_can_win_on_the_next_turn { [TestMethod] public void it_should_block_the_player() { } }
As always, we’ll start by deciding what observable outcome we want to depend upon to know the behavior is working correctly. Since we’re expecting the game to block the player, let’s come up with a scenario we know wouldn’t result from the existing behavior of trying to get three in a row. Let’s say the player goes first and has one position left in the center vertical row to win:
To validate this scenario, we’ll check that the GameAdvisor chooses the eighth position:
[TestClass] public class When_the_player_can_win_on_the_next_turn { [TestMethod] public void it_should_block_the_player() { Assert.AreEqual(8, selection); } }
Next, let’s setup the rest of the context to declare the instance of our SUT and establish the layout:
[TestClass] public class When_the_player_can_win_on_the_next_turn { [TestMethod] public void it_should_block_the_player() { IGameAdvisor advisor = new GameAdvisor(); int selection = advisor.WithLayout("XOX").SelectBestMoveForPlayer('O'); Assert.AreEqual(8, selection); } }
Now, let’s run our test:
When_the_player_can_win_on_the_next_turn Failed it_should_block_the_player Assert.AreEqual failed. Expected:<8>. Actual:<1>.
Now, let’s make the test pass. This time, I’ll pass the test by testing specifically for the layout we’re after:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } if (layout == "XTX") { return 8; } int[] bestSlice = availablePaths .OrderByDescending(path => path .Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Now, let’s refactor. To get the GameAdvisor to choose the eighth position because it recognizes it’s vulnerable to losing, we’ll need to check how close the player is. To do this, we can find all of the available paths for the player and check if any of them already have two positions occupied. First, we’ll need to know which token the player is using:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } char opponentValue = (player == 'X') ? 'O' : 'X'; if (layout == "XTX") { return 8; } int[] bestSlice = availablePaths .OrderByDescending(path => path .Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); } }
Next, let’s create a new local layout based on the player’s positions:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } char opponentValue = (player == 'X') ? 'O' : 'X'; string playerLayout = _layout.Replace(opponentValue, 'T'); if (layout == "XTX") { return 8; } int[] bestSlice = availablePaths .OrderByDescending(path => path .Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Now, let’s copy the logic we created before and use it to find the available paths for the opponent:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => opponentLayout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availableOpponentPaths.Add(winningSlice); } if (layout == "XTX") { return 8; } int[] bestSlice = availablePaths .OrderByDescending(path => path .Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Lastly, let’s find all the available paths for which the opponent already has two positions filled and remove our fake implementation:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => opponentLayout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availableOpponentPaths.Add(winningSlice); } int[] threatingPath = availableOpponentPaths .Where(path => new string( path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath .First(position => opponentLayout[position - 1] == '\0'); } int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Let’s run our test and see what happens:
When_the_player_gets_three_in_a_row Failed it_should_announce_the_player_as_the_winner Assert.AreEqual failed. Expected:<Player wins!>. Actual:<That spot is taken!>.
Our test still passes, but for some reason we broke the test for testing that the player wins when getting three in a row. Let’s have a look:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(); game.ChoosePosition(4); game.ChoosePosition(5); string message = game.ChoosePosition(6); Assert.AreEqual("Player wins!", message); } }
We wrote this test to assume we could pick positions without worrying about getting blocked. That behavior has changed, so we’ll need to adapt our test. Again, we can use our new seam to plug in the path we want the Game to follow to stay out of our way:
[TestClass] public class When_the_player_gets_three_in_a_row { [TestMethod] public void it_should_announce_the_player_as_the_winner() { var game = new Game(new GameAdvisorStub(new int[] { 1, 2, 3})); game.ChoosePosition(4); game.ChoosePosition(5); string message = game.ChoosePosition(6); Assert.AreEqual("Player wins!", message); } }
Now that we’ve fixed that test, let’s continue our refactoring effort. In generalizing our code, we introduced some duplication. Let’s fix this by extracting a method for determining the available paths for a given player:
List<int[]> GetAvailablePathsFor(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } return availablePaths; }
Now our SelectBestPlayerFor() method becomes:
public int SelectBestMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = GetAvailablePathsFor(player); char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string( path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath .First(position => opponentLayout[position - 1] == '\0'); } int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Next, let’s reorganize some of these operations so we can see how things group together:
public int SelectBestMoveForPlayer(char player) { char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string( path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath .First(position => opponentLayout[position - 1] == '\0'); } string layout = _layout.Replace(player, 'T'); List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
The first section is all about checking for threating opponent paths, but this isn’t very descriptive at the moment. Let’s move all of that into a method that describes exactly what we’re doing:
public int SelectBestMoveForPlayer(char player) { int? threatingPosition = GetPositionThreateningPlayer(player); if (threatingPosition != null) return threatingPosition.Value; string layout = _layout.Replace(player, 'T'); List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); } int? GetPositionThreateningPlayer(char player) { char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string( path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath .First(position => opponentLayout[position - 1] == '\0'); } return null; }
Next, let’s extract the code for selecting the next winning path position for the player into a separate method:
public int SelectBestMoveForPlayer(char player) { int? threatingPosition = GetPositionThreateningPlayer(player); if (threatingPosition != null) return threatingPosition.Value; return GetNextWinningMoveForPlayer(player); } int GetNextWinningMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); }
Now, we can reduce our SelectBestMoveForPlayer() method down to one fairly descriptive line:
public int SelectBestMoveForPlayer(char player) { return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); }
We’re done! Here’s our GameAdvisor implementation:
class GameAdvisor : IGameAdvisor { public IPositionSelector WithLayout(string layout) { return new PositionSelector(layout); } class PositionSelector : IPositionSelector { static readonly int[][] _winningPositions = new[] { new[] {1, 2, 3}, new[] {4, 5, 6}, new[] {7, 8, 9}, new[] {1, 4, 7}, new[] {2, 5, 8}, new[] {3, 6, 9}, new[] {1, 5, 9}, new[] {3, 5, 7}, }; readonly Regex _availablePathPattern = new Regex(@"[T]{3}"); readonly string _layout; public PositionSelector(string layout) { _layout = layout; } public int SelectBestMoveForPlayer(char player) { return GetPositionThreateningPlayer(player) ?? GetNextWinningMoveForPlayer(player); } int GetNextWinningMoveForPlayer(char player) { string layout = _layout.Replace(player, 'T'); List<int[]> availablePaths = GetAvailablePathsFor(player); int[] bestSlice = availablePaths.OrderByDescending( path => path.Count(p => layout[p - 1] == 'T')).First(); return bestSlice.First(p => _layout[p - 1] == '\0'); } int? GetPositionThreateningPlayer(char player) { char opponentValue = (player == 'X') ? 'O' : 'X'; string opponentLayout = _layout.Replace(opponentValue, 'T'); List<int[]> availableOpponentPaths = GetAvailablePathsFor(opponentValue); int[] threatingPath = availableOpponentPaths .Where(path => new string( path.Select(p => opponentLayout[p - 1]).ToArray()) .Count(c => c == 'T') == 2).FirstOrDefault(); if (threatingPath != null) { return threatingPath .First(position => opponentLayout[position - 1] == '\0'); } return null; } List<int[]> GetAvailablePathsFor(char player) { string layout = _layout.Replace(player, 'T'); var availablePaths = new List<int[]>(); foreach (var winningSlice in _winningPositions) { var slice = new string(winningSlice.ToList() .Select(p => layout.ElementAt(p - 1)).ToArray()); if (_availablePathPattern.IsMatch(slice)) availablePaths.Add(winningSlice); } return availablePaths; } } }
While we’ve been working on our component, another team has been putting together a host application with a nice user interface. We’re now ready to hand our component over so it can be integrated into the rest of the application. Afterward, the full application will be passed on to a Quality Assurance Team to receive some acceptance testing. Next time we’ll take a look at any issues that come out of the integration and QA testing processes.



