title: Shouldn't NSUserDefault be clean slate for unit tests?
body: <p>I'm writing my first iOS unit tests (Xcode 5, iOS 6) and find that the results of the unit tests vary depending on what I've done in the Simulator lately.  E.g. I click on a user in my contacts list in the Simulator, and now my "recent contacts" data in UserDefaults has one more object than before, even when I'm running unit tests.</p>
  
  <p>For unit testing, it's not clean to have random user defaults data (I'm used to RoR tests with their own clean db). Besides, I might want to test specific states like having empty "recent contacts" data.</p>
  
  <p>From looking at related questions here, I seem some possible answers that I'm not happy with. </p>
  
  <ul>
  <li>Mock UserDefaults for the unit tests! I would have to modify many existing classes so that I can inject that mock.  </li>
  <li>Clear or customize UserDefaults in a setUp method!  But then my data created laboriously in manual testing would be gone.</li>
  <li>Clear or customize UserDefaults in a setUp method <strong>then</strong> restore those values in tearDown!  Ouch.</li>
  </ul>
  
  <p>These seem unnecessarily complicated for something that should be standard practice in unit tests.  I don't want to repeat myself in every unit test.  So, my questions are:</p>
  
  <ul>
  <li>Am I missing something desirable about the way UserDefaults are persisted from ad-hoc Simulator testing through to unit test runs?</li>
  <li>Is there a configurable way to fix this, say some way to set the unit test target to have different storage location for UserDefaults than when I use the Simulator to manually test?</li>
  <li>Failing that, is there an elegant way to do this in code?  </li>
  <li>For example,  I could have a MyAppTestCase object inherit from XCTestCase and override setUp and tearDown methods to always set aside then restore the UserDefaults.  Is this a good idea?</li>
  </ul>