Skip to content

Instantly share code, notes, and snippets.

@fiunchinho
Created December 2, 2013 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiunchinho/7757712 to your computer and use it in GitHub Desktop.
Save fiunchinho/7757712 to your computer and use it in GitHub Desktop.
Triying to describe in PHPSpec that using wrong parameters throws exception
<?php
namespace MyApp;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class UserSpec extends ObjectBehavior
{
function it_fails_when_using_a_not_valid_email()
{
// Needs to be constructed with a valid email first :(:(
$this->beConstructedWith( 'valid@email.com' );
$this->shouldThrow( '\InvalidArgumentException' )->during( '__construct', array( 'wrong_email' ) );
}
}
@fiunchinho
Copy link
Author

Since PHPSpec creates the object before hand, you need to first instantiate the object with valid arguments. Otherwise the exception will be thrown BEFORE being expected, so phpspec will complain because

exception [exc:InvalidArgumentException("You must enter a valid email")] has been thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment