– Edited by following comments in answer –
If I'm understanding what you are trying to do properly, you could do this:
Mock thecreate()
method to return a mockedProject
.- Mock the
save()
method to return false. - Mock a
MessageBag
instance, which is the object thaterrors()
would return. This mock should have a mockedall()
method. - Make the mocked
errors()
method to return theMessageBag
mock.
Assuming $this->mock
is a mock of the Project
class here:
// given that $this->mock = Mockery::mock('Project')
public function testStoreFails()
{
// Mock the create() method to return itself
$this->mock->shouldReceive('save')->once()->andReturn(false);
// Mock MessageBag and all() method
$errors = Mockery::mock('Illuminate\Support\MessageBag');
$errors->shouldReceive('all')->once()->andReturn(array('foo' => 'bar'));
// Mock errors() method from model and make it return the MessageBag mock
$this->mock->shouldReceive('errors')->andReturn($errors);
// Proceed to make the post call
$this->call('POST', 'projects');
$this->assertRedirectedToRoute('projects.create');
$this->assertSessionHasErrors();
}