In the previous example, most of the attributes are public; however, the ID of the bookmark is protected to avoid accidents changing its value (this would be problematic when you wanted to later update the bookmark). Since $id is protected, add an accessor method to retrieve it from the Bookmark. class Bookmark { How do you test this? class ActiveRecordTestCase extends UnitTestCase { Immediately above, add() persists several bookmarks and verifies that the latter of the two matches. So far, so good, but what if you want to verify the database entry based on a different criteria than the bookmark ID? How can you make sure the correct ID from the database is being returned? A good technique is to SELECT from the database using a known attribute and verify the ID from the returned row. Here’s a test using this methodology: class ActiveRecordTestCase extends UnitTestCase { Notice that this test resembles the SQL you might execute manually to verify the insertion of the data into the bookmark table. By coding this as a test, rather than simply performing it once by hand, you can continue to verify it is taking place each time you run the tests.
blog comments powered by Disqus |