Unit Testing Active record database Mock


Is there something to Mock the database ressource while testing an Active Record Model ?


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.

1 Like

Figured it out…


	->setMethods(array("getIsNewRecord", "save"))



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