Hi,
Is there something to Mock the database ressource while testing an Active Record Model ?
thanks
Hi,
Is there something to Mock the database ressource while testing an Active Record Model ?
thanks
I’m looking for this as well. When I try to mock an ActiveRecord class I get a “Trying to get property of non-object” error in my test.
Seems strange that there is literally zero documentation on the internet around how to do this…
I understand the usage of fixtures however it doesn’t seem right to have to set up a database to test just one component. I’d like to test my component in isolation from a database.
Figured it out…
$this->getMockBuilder("Translation")
->setMethods(array("getIsNewRecord", "save"))
->disableOriginalConstructor()
->getMock();
Where “Translation” is the name of my active record model. You MUST use setMethods to identify the methods you’re going to be mocking so that the rest of the methods belonging to the active record will remain intact. This is important so that the default __get and __set magic methods will still work properly.
"disableOriginalConstructor" is required to properly construct the activerecord object