public function testActionPostErrorSave() { $this->_eventManager->on('Dispatcher.invokeController', ['priority' => 1000], function ($event) { $this->_controller->Flash = $this->getMockBuilder('Cake\\Controller\\Component\\FlashComponent')->setMethods(['set'])->disableOriginalConstructor()->getMock(); $this->_controller->Flash->expects($this->once())->method('set')->with('Could not create blog', ['element' => 'default', 'params' => ['class' => 'message error', 'original' => 'Could not create blog'], 'key' => 'flash']); $this->_subscribeToEvents($this->_controller); $this->_controller->Blogs = $this->getMockForModel($this->tableClass, ['save'], ['alias' => 'Blogs', 'table' => 'blogs']); $this->_controller->Blogs->expects($this->once())->method('save')->will($this->returnValue(false)); }); $this->post('/blogs/add', ['name' => 'Hello World', 'body' => 'Pretty hot body']); $this->assertEvents(['beforeSave', 'afterSave', 'setFlash', 'beforeRender']); $this->assertFalse($this->_subject->success); $this->assertFalse($this->_subject->created); }