Last active
January 1, 2016 22:19
-
-
Save yegnold/8208710 to your computer and use it in GitHub Desktop.
An example of how I'm trying to verify that sometimes() is called twice using Laravel's testing stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ValidatableModelTest extends TestCase { | |
public $validatable_model; | |
public function setUp() { | |
$this->validatable_model = new ValidatableModel; | |
} | |
/** | |
* Utility method to get valid sometimes rules which are used in multiple tests | |
*/ | |
protected function getValidSometimesRules() { | |
return array( | |
array('first_name', 'min:4', function($input) { return $input->name != 'Joe'; } ), | |
array('last_name', 'min:4', function($input) { return $input->name != 'Smo'; } ) | |
); | |
} | |
/** | |
* If we set sometimes rules on our validatable model, | |
* when we call validate() on that model we need to make sure | |
* that sometimes() is called on the validator object | |
*/ | |
public function testSometimesIsInvoked() { | |
$this->validatable_model->setSometimesRules($this->getValidSometimesRules()); | |
$mocked_validator = Mockery::mock(); | |
$mocked_validator->shouldReceive('sometimes')->atLeast()->times(2); | |
$mocked_validator->shouldReceive('fails')->once()->andReturn(false); | |
Validator::shouldReceive('make')->once()->andReturn($mocked_validator); | |
$this->assertTrue($this->validatable_model->validate(array('first_name' => 'Hello', 'last_name' => 'Mate'))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My ValidatableModel is not calling sometimes() twice, yet this test passes silently with no complaints? Am I doing it wrong?!