Skip to content

Instantly share code, notes, and snippets.

@yegnold
Last active January 1, 2016 22:19
Show Gist options
  • Save yegnold/8208710 to your computer and use it in GitHub Desktop.
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
<?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')));
}
}
@yegnold
Copy link
Author

yegnold commented Jan 1, 2014

My ValidatableModel is not calling sometimes() twice, yet this test passes silently with no complaints? Am I doing it wrong?!

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