hardayal / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Create class to encapsulate both the value and description #150

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
First allow an object instance value to describe itself by implementing an 
interface such as Hamcrest_Describable.

Next implement this interface to create a simple value-and-description-holder 
that can be returned from test fixture methods.

For example, we want to make assertions about values in the Zend Registry. 
Currently we do

    assertThat($this->registry->get('siteId'), is('home'));

but the failure message doesn't reference 'siteId'. I can write a custom 
description, but I'd have to do that every time. I want to call a test helper 
method that a) creates the description and b) pulls the registry value. The 
only way to do that is combine them into a single object:

    assertThat($this->getRegistryValue('siteId'), is('home'));

    function getRegistryValue($key) {
        return new Hamcrest_DescribedValue(
                "registry entry $key", 
                $this->registry->get($key)
            );
    }

Of course, I could have the helper call assertThat() directly:

    function assertRegistry($key, $matcher) {
        assertThat(
                "registry entry $key", 
                $this->registry->get($key), 
                $matcher
            );
    }

Original issue reported on code.google.com by dharkn...@gmail.com on 29 Apr 2011 at 5:57