# Path: doubles/instance_double.py
# class InstanceDouble(ObjectDouble):
#     """A pure double representing an instance of the target class.
# 
#     Any kwargs supplied will be set as attributes on the instance that is
#     created.
# 
#     ::
# 
#         user = InstanceDouble('myapp.User', name='Bob Barker')
# 
#     :param str path: The absolute module path to the class.
#     """
# 
#     def __init__(self, path, **kwargs):
#         module_path, class_name = get_path_components(path)
#         module = get_module(module_path, path)
#         self._doubles_target = _get_doubles_target(module, class_name, path)
#         for k, v in kwargs.items():
#             setattr(self, k, v)
# 
# Path: doubles/targets/allowance_target.py
# def allow(target):
#     """
#     Prepares a target object for a method call allowance (stub). The name of the method to allow
#     should be called as a method on the return value of this function::
# 
#         allow(foo).bar
# 
#     Accessing the ``bar`` attribute will return an ``Allowance`` which provides additional methods
#     to configure the stub.
# 
#     :param object target: The object that will be stubbed.
#     :return: An ``AllowanceTarget`` for the target object.
#     """
# 
#     return AllowanceTarget(target)
# 
# Path: doubles/targets/expectation_target.py
# def expect(target):
#     """
#     Prepares a target object for a method call expectation (mock). The name of the method to expect
#     should be called as a method on the return value of this function::
# 
#         expect(foo).bar
# 
#     Accessing the ``bar`` attribute will return an ``Expectation`` which provides additional methods
#     to configure the mock.
# 
#     :param object target: The object that will be mocked.
#     :return: An ``ExpectationTarget`` for the target object.
#     """
# 
#     return ExpectationTarget(target)
# 
# Path: doubles/lifecycle.py
# def teardown():
#     """Tears down the current Doubles environment. Must be called after each test case."""
#     if hasattr(_thread_local_data, 'current_space'):
#         _thread_local_data.current_space.teardown()
#         del _thread_local_data.current_space


from pytest import raises, mark
from doubles.instance_double import InstanceDouble
from doubles import allow, expect
from doubles.lifecycle import teardown
        assert subject.method_with_varkwargs(bob='barker') == 'barker'

    def test_raises_provided_exception(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_raise(UserDefinedException)

        with raises(UserDefinedException):
            subject.instance_method()

    def test_raises_provided_exception_with_complex_signature(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_raise(
            UserDefinedExceptionWithArgs('msg', 'arg1', arg2='arg2'),
        )

        with raises(UserDefinedExceptionWithArgs):
            subject.instance_method()

    def test_chaining_result_methods_gives_the_last_one_precedence(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_return('bar').and_return_result_of(
            lambda: 'baz'
        ).and_raise(UserDefinedException).and_return('final')

        assert subject.instance_method() == 'final'



# Path: doubles/instance_double.py
# class InstanceDouble(ObjectDouble):
#     """A pure double representing an instance of the target class.
# 
#     Any kwargs supplied will be set as attributes on the instance that is
#     created.
# 
#     ::
# 
#         user = InstanceDouble('myapp.User', name='Bob Barker')
# 
#     :param str path: The absolute module path to the class.
#     """
# 
#     def __init__(self, path, **kwargs):
#         module_path, class_name = get_path_components(path)
#         module = get_module(module_path, path)
#         self._doubles_target = _get_doubles_target(module, class_name, path)
#         for k, v in kwargs.items():
#             setattr(self, k, v)
# 
# Path: doubles/targets/allowance_target.py
# def allow(target):
#     """
#     Prepares a target object for a method call allowance (stub). The name of the method to allow
#     should be called as a method on the return value of this function::
# 
#         allow(foo).bar
# 
#     Accessing the ``bar`` attribute will return an ``Allowance`` which provides additional methods
#     to configure the stub.
# 
#     :param object target: The object that will be stubbed.
#     :return: An ``AllowanceTarget`` for the target object.
#     """
# 
#     return AllowanceTarget(target)
# 
# Path: doubles/targets/expectation_target.py
# def expect(target):
#     """
#     Prepares a target object for a method call expectation (mock). The name of the method to expect
#     should be called as a method on the return value of this function::
# 
#         expect(foo).bar
# 
#     Accessing the ``bar`` attribute will return an ``Expectation`` which provides additional methods
#     to configure the mock.
# 
#     :param object target: The object that will be mocked.
#     :return: An ``ExpectationTarget`` for the target object.
#     """
# 
#     return ExpectationTarget(target)
# 
# Path: doubles/lifecycle.py
# def teardown():
#     """Tears down the current Doubles environment. Must be called after each test case."""
#     if hasattr(_thread_local_data, 'current_space'):
#         _thread_local_data.current_space.teardown()
#         del _thread_local_data.current_space


# Path: test/return_values_test.py
from pytest import raises, mark
from doubles.instance_double import InstanceDouble
from doubles import allow, expect
from doubles.lifecycle import teardown

        assert subject.method_with_varkwargs(bob='barker') == 'barker'

    def test_raises_provided_exception(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_raise(UserDefinedException)

        with raises(UserDefinedException):
            subject.instance_method()

    def test_raises_provided_exception_with_complex_signature(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_raise(
            UserDefinedExceptionWithArgs('msg', 'arg1', arg2='arg2'),
        )

        with raises(UserDefinedExceptionWithArgs):
            subject.instance_method()

    def test_chaining_result_methods_gives_the_last_one_precedence(self, stubber):
        subject = InstanceDouble('doubles.testing.User')

        stubber(subject).instance_method.and_return('bar').and_return_result_of(
            lambda: 'baz'
        ).and_raise(UserDefinedException).and_return('final')

        assert subject.instance_method() == 'final'



@mark.parametrize('stubber', [allow, expect])