def test_flex_respects_at_most_when_called_requested_number(self): class Foo: def method_foo(self): pass foo = Foo() flex(foo).method_foo.returns('value_bar').times(0, 1) foo.method_foo() verify()
def test_flex_respects_at_most_when_called_less_than_requested(self): class Foo: def method_foo(self): pass foo = Foo() flex(foo).method_foo.returns('bar').times(0, 2) foo.method_foo() verify()
def test_flex_respects_at_least_when_called_more_than_requested(self): class Foo: def method_foo(self): pass foo = Foo() flex(foo).method_foo.returns('value_bar').times(1, None) foo.method_foo() foo.method_foo() verify()
def test_should_properly_restore_undecorated_static_methods(self): class User: def get_stuff(): return 'ok!' get_stuff = staticmethod(get_stuff) assertEqual('ok!', User.get_stuff()) flex(User).get_stuff assert User.get_stuff() is None verify() assertEqual('ok!', User.get_stuff())
def test_flex_should_properly_restore_module_level_functions(self): if 'flex_test' in sys.modules: mod = sys.modules['flex_test'] else: mod = sys.modules['__main__'] flex(mod).module_level_function assertEqual(None, module_level_function(1, 2)) verify() assertEqual('1, 2', module_level_function(1, 2))
def test_flex_preserves_stubbed_class_methods_between_tests(self): class User: def get_name(self): return 'mike' user = User() flex(User).get_name.returns('john') assertEqual('john', user.get_name()) verify() assertEqual('mike', user.get_name())
def test_flex_should_properly_restore_static_methods(self): class User: @staticmethod def get_stuff(): return 'ok!' assertEqual('ok!', User.get_stuff()) flex(User).get_stuff assert User.get_stuff() is None verify() assertEqual('ok!', User.get_stuff())
def test_flex_should_revert_new_instances_on_teardown(self): class User(object): pass class Group(object): pass user = User() group = Group() flex(Group).__new__.returns(user) assert user is Group() verify() assertEqual(group.__class__, Group().__class__)
def test_flex_should_properly_restore_class_methods(self): class User: @classmethod def get_stuff(cls): return cls.__name__ assertEqual('User', User.get_stuff()) flex(User).get_stuff.returns('foo') assertEqual('foo', User.get_stuff()) verify() assertEqual('User', User.get_stuff())
def test_calls_works_for_same_method_with_different_args(self): class Foo: def method(self, arg): pass foo = Foo() flex(foo).method('foo').runs().times(1) flex(foo).method('bar').runs().times(1) foo.method('foo') foo.method('bar') verify()
def test_flex_removes_new_stubs_from_classes_after_tests(self): class User: def get_name(self): pass user = User() saved = user.get_name flex(User).get_name.returns('john') assert saved != user.get_name assertEqual('john', user.get_name()) verify() assertEqual(saved, user.get_name)
def test_flex_default_calls_respects_matched_expectations(self): class Group(object): def method1(self, arg1, arg2='b'): return '%s:%s' % (arg1, arg2) def method2(self, arg): return arg group = Group() flex(group).method1.runs().times(2) assertEqual('a:c', group.method1('a', arg2='c')) assertEqual('a:b', group.method1('a')) flex(group).method2('c').runs().times(1) assertEqual('c', group.method2('c')) verify()
def test_support_at_least_and_at_most_together_inside_range(self): class Foo: def bar(self): pass foo = Foo() flex(foo).bar.runs().times(1, 2) foo.bar() verify() flex(foo).bar.runs().times(1, 2) foo.bar() foo.bar() verify()
def test_flex_should_match_keyword_arguments(self): class Foo: def method1(self): pass foo = Foo() flex(foo).method1(1, arg3=3, arg2=2).times(2) foo.method1(1, arg2=2, arg3=3) foo.method1(1, arg3=3, arg2=2) verify() flex(foo).method1(1, arg3=3, arg2=2) assertRaises(MethodSignatureError, foo.method1, arg2=2, arg3=3) flex(foo).method1(1, arg3=3, arg2=2) assertRaises(MethodSignatureError, foo.method1, 1, arg2=2, arg3=4) flex(foo).method1(1, arg3=3, arg2=2) assertRaises(MethodSignatureError, foo.method1, 1)
def test_flex_removes_stubs_from_multiple_classes_on_teardown(self): class User: def get_name(self): pass class Group: def get_name(self): pass user = User() group = User() saved1 = user.get_name saved2 = group.get_name flex(User).get_name.returns('john') flex(Group).get_name.returns('john') assert saved1 != user.get_name assert saved2 != group.get_name assertEqual('john', user.get_name()) assertEqual('john', group.get_name()) verify() assertEqual(saved1, user.get_name) assertEqual(saved2, group.get_name)
def _tear_down(self): return verify()
def test_new_instances_should_be_a_method(self): class Foo(object): pass flex(Foo).__new__.returns('bar') assertEqual('bar', Foo()) verify() assert 'bar' != Foo()
def test_flex_works_with_never_when_true(self): class Foo: def method_foo(self): pass foo = Foo() flex(foo).method_foo.returns('value_bar').times(0) verify()
def test_flex_teardown_does_not_verify_stubs(self): class Foo: def uncalled_method(self): pass flex(Foo).uncalled_method() verify()