Mocking default=timezone.now for unit tests

Here’s a method you can use that doesn’t require altering your non-test code. Just patch the default attributes of the fields you want to affect. For example– field = User._meta.get_field(‘timestamp’) mock_now = lambda: datetime(2010, 1, 1) with patch.object(field, ‘default’, new=mock_now): # Your code here You can write helper functions to make this less verbose. For … Read more

Mock class in class under test

You could refactor MyClass so that it uses dependency injection. Instead of having it create an AnythingPerformerClass instance you could pass in an instance of the class to the constructor of MyClass like so : class MyClass { private final AnythingPerformerClass clazz; MyClass(AnythingPerformerClass clazz) { this.clazz = clazz; } public boolean performAnything() { return clazz.doSomething(); … Read more

How to get Resharper to allow underscores in method names but only in Tests?

This post demonstrates how to configure this: http://atombrenner.blogspot.com/2010/07/how-to-change-resharper-naming-style.html UPDATE: The Machine.Specifications testing framework extends ReSharper to allow for custom naming styles for the tests/specs. Very cool; wish other frameworks did likewise. http://lostechies.com/derekgreer/2010/02/11/resharper-naming-style-for-machine-specifications/