Writing specs for a class that behaves differently depending upon constructor arguments

StackOverflow https://stackoverflow.com/questions/16193277

  •  11-04-2022
  •  | 
  •  

Question

If you have a class that responds differently depending upon constructor arguments, how do you go about writing a spec for that class?

class Route
{
  function __construct($url, array $methods = array())
  {
    // stores methods and url in private member variables
    // creates a regex to match $url against incoming request URLs
  }

  public function isMatch($url)
  {
    // checks if the incoming request url matches against this url
  }
}

Example use:

$a = new Route('/users/:id');
$a->isMatch('/users/1') // returns true;
$b = new Route('/users');
$b->isMatch('/users') // returns true

If I set up my spec for this class using the let function from phpspec:

class Route extends ObjectBehaviour
{
  function let() 
  {
    $this->beConstructedWith('/users/:id')
  }
}

My spec can only check if the behaviour of this class works in one of the cases.

I've contemplated adding setter methods to allow me to test around this, but it seems like I'd be breaking encapsulation for the purpose of testing.

I'm struggling to find anything that touches upon this, so I'm started to think that maybe this is bad code smell situation.

Was it helpful?

Solution 2

  1. Constructor should be used only to obtain variables that will be set to a member properties here. No further logic should be done here...
  2. Following the idea from point 1 there should be another logic that determines what happens next (e.g. if Object->hasProperty(X) then do x(), etc.)
  3. Then a comment would be plain and straight forward.

Example:

class Route
{
    private $url;
    private $methods = array();

    /**
     * Constructor method, sets the attributes to private member variables
     * @param string $url URL pattern
     * @param array $methods Methods that should be used with given URL
     */
    function __construct($url, $methods = array())
    {
        $this->url      = $url;
        $this->methods  = $methods;
    }

    // ...

}

OTHER TIPS

beConstructedWith() doesn't always need to be called from the let() method. You can call it from the specs as well.

In my opinion there's nothing wrong in setting up an object in more than one way. However, you should avoid doing too much work in the constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top