I'm studying how to use mocking in my unit test program.
Now I have a SafeConfigParser object and I want to test what I write is correct.
After google the mocking usage of SafeConfigParser, I already know how to test the read of SafeConfigParser. But I still don't know how to verify the write of SafeConfigParser.
My idea is:
- Make a empty buffer.
- Consider a method that can set the buffer to
SafeConfigParser. - Call the function which include
SafeConfigParser.write() - Verify the buffer with my answer.
My program which need to be tested is like following:
def write_tokens_to_config(self): """Write token values to the config """ parser = SafeConfigParser() with open(self.CONFIG_PATH) as fp: parser.readfp(fp) if not parser.has_section('Token'): parser.add_section('Token') parser.set('Token', 'access_token', self._access_token) parser.set('Token', 'refresh_token', self._refresh_token) with open(self.CONFIG_PATH, 'wb') as fp: parser.write(fp) P.S. You can check the read part from this url: http://www.snip2code.com/Snippet/4347/