0

In phpspec i can test something like this:

function it_must_be_constructed_with_my_variable()
{
    $this->shouldThrow(new \Exception('bla'))->during('__construct', array('variable' => 'value'));
}

But what if i wanted to make sure an exception is thrown if something is not passed to the function?

I.e. i want to say that an exception will be thrown if the array passed is not equals to some value.

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • Is the constructor a valid target for specification testing? But nonetheless your question can be reworded: How to test that something happens if a parameter is not passed? My first guess would be to either pass an empty parameter array, or leave it out entirely. – Sven Nov 09 '13 at 09:57

2 Answers2

1

I believe you can simply do something like:

function it_should_throw_exception_if_constructed_with_wrong_variable()
{
    $myWrongVariable = array('something' => 'wrong');
    $this->shouldThrow(new \Exception('bla'))->during('__construct', array($myWrongVariable));
}
Ciaran McNulty
  • 18,698
  • 6
  • 32
  • 40
Carl Owens
  • 1,292
  • 8
  • 14
1

If I understand you right, you want to test that an excpetion is thrown when the user is passing an array the implementation is not aware of or is not able to handle.

Then you could just take your example and pass the "Wrong" array as parameter

CansasCity
  • 47
  • 2
  • 3