I've just started using Phpspec on my Laravel project.
The class I'm testing has the following schema:
class Card
{
/**
* @var int
*/
private $rank;
/**
* @var int
*/
private $suit;
/**
* @param int $rank
* @param int $suit
*/
public function __construct($rank = null, $suit = null);
/**
* @return int
*/
public function getRank();
/**
* @return int
*/
public function getSuit();
/**
* @param int $rank
*/
private function setRank($rank);
/**
* @param int $suit
*/
private function setSuit($suit);
}
I didn't include the actual code as it is quite simplistic. It throws exceptions in case parameters are invalid, but let me know if you'd like me to update this with code. And here's the spec class:
class CardSpec extends ObjectBehavior
{
const INVALID_PARAM = 'WontWork';
const INVALID_RANK = 14;
const INVALID_SUIT = 5;
const VALID_RANK = 12;
const VALID_SUIT = 2;
function it_is_initializable()
{
$this->shouldHaveType('Shazzam\Games\Blackjack\Card');
}
function it_throws_exception_on_invalid_arguments()
{
$this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::INVALID_PARAM, 0]);
$this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::INVALID_RANK, self::VALID_SUIT]);
$this->shouldThrow('\InvalidArgumentException')->during('__construct', [self::VALID_RANK, self::INVALID_SUIT]);
}
function it_creates_an_object_successfully()
{
// Would like to pass parameters and check its values using the get methods
}
}
I've been able to check it is throwing an exception for invalid arguments, but now I want to make sure it is passing the construct validation and returning the values when valid.
I've read the documentation and couldn't find a way to achieve this, which leads me to believe that I'm not fully understanding BDD.
Is this kind of test a unit test and should therefore be separate from behavior tests?
After some more reading I've come across this nice post:
https://laracasts.com/discuss/channels/general-discussion/codeception-phpspec-acceptance-functional-integration-unit-testing
Check austenc and ozanhazer answers if you are having the same doubts.
As in that post, phpspec isn't meant for functional and acceptance tests.
Check that post or google to match the right tools with the kinds of tests you want to implement.