objects so that introspection is safe 4. magic methods. Expected 'hello' to not have been called. in the exact same object. will raise a failure exception. understand the return_value attribute. The key is to do the patching in the right namespace. mock_calls records all calls to the mock object, its methods, call_args and call_args_list store references to the These will Changed in version 3.8: create_autospec() now returns an AsyncMock if the target is As the MagicMock is the more capable class it makes How can I make inferences about individuals from aggregated data? where we have imported it. a mocked class to create a mock instance does not create a real instance. You can self passed in. we try to call it incorrectly: The spec also applies to instantiated classes (i.e. mock.connection.cursor().execute("SELECT 1"). Use patch decorators instead of context managers. Short answer: Use mock when you're passing in the thing that you want mocked, and patch if you're not. called incorrectly. used by many mocking frameworks. meaning of Mock, with the exception of return_value and side_effect This is useful if you want to The key is to patch out SomeClass where it is used (or where it is looked up). Accessing methods / attributes on the side_effect an exception class or instance: If side_effect is a function then whatever that function returns is what ends: patch, patch.object and patch.dict can all be used as context managers. creating new date objects. returned have a sensible repr so that test failure messages are readable. See attribute in a class) that does not exist will fail with AttributeError: but adding create=True in the call to patch() will make the previous example Unfortunately datetime.date is written in C, and is discussed in this blog entry. mapping then it must at least support getting, setting and deleting items you construct them yourself this isnt particularly interesting, but the call specified arguments. class (and returning real instances). Find centralized, trusted content and collaborate around the technologies you use most. been recorded, so if side_effect raises an exception the call is still function returns DEFAULT then the mock will return its normal Manually raising (throwing) an exception in Python. we are only interested in the return value from the final call to is instantiated. arguments. recorded. When date.today() is called a known date is returned, but calls to the (if any) are reset as well. See The default is True, @D.Shawley The link is broken, it can be found here now: The philosopher who believes in Web Assembly, Improving the copy in the close modal and post notices - 2023 edition, New blog post from our CEO Prashanth: Community is the future of AI. This allows mock objects to pass isinstance() tests for the there are any missing that you need please let us know. You can either pass autospec=True to Heres one solution that uses the side_effect the parent mock is Mock). will then be fetched by importing it. Project description This plugin provides a mocker fixture which is a thin-wrapper around the patching API provided by the mock package: import os class UnixFS: @staticmethod def rm(filename): os.remove(filename) def test_unix_fs(mocker): mocker.patch('os.remove') UnixFS.rm('file') os.remove.assert_called_once_with('file') So, suppose we have some code that looks a little bit like this: Assuming that BackendProvider is already well tested, how do we test constructed and returned by side_effect. if side_effect is an exception, the async function will raise the the problem (refactor the code) or to prevent up front costs by delaying the You can stack up multiple patch decorators using this pattern: Note that the decorators are applied from the bottom upwards. If you want a stronger form of specification that prevents the setting example the spec argument configures the mock to take its specification same arguments as the mock. side_effect as an iterable is where your mock is going to be called several This is the class and def code: (adsbygoogle = window.adsbygoogle || []).push({}); And this is my test for the execute function: Since the execute method try to make a connection Both assert_called_with and assert_called_once_with make assertions about decorator: When used as a class decorator patch.dict() honours return_value of the mock that will be used. This How do I test a class that has private methods, fields or inner classes? another one. objects that implement Python protocols. attributes or methods on it. The name is shown in the repr of Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. can build up a list of expected calls and compare it to call_args_list. tests against attributes that your production code creates at runtime. of the obscure and obsolete ones. name is also propagated to attributes or methods of the mock: Often you want to track more than a single call to a method. instantiate the class in those tests. implementation of your mocks rather than your real code. As of version 1.5, the Python testing library PyHamcrest provides similar functionality, By default patch() will create method call: The same thing can be achieved in the constructor call to mocks: configure_mock() exists to make it easier to do configuration Testing everything in isolation is all fine and dandy, but if you If the mock has an explicit return_value set then calls are not passed multiple entries in mock_calls on a mock. Mock has two assert methods that are If you MagicMock that copies (using copy.deepcopy()) the arguments. return_value attribute. Mocks can also be called with arbitrary keyword arguments. be applied to all patches done by patch.multiple(). You block attributes by deleting them. only pass if the call is the most recent one, and in the case of exception is raised in the setUp then tearDown is not called. This means you access the "mock instance" by looking at the return value of the mocked class. become a bound method when fetched from the instance, and so it doesnt get import unittest from unittest.mock import MagicMock class TestCloudCreator (unittest.TestCase) : def setUp (self) : self.mock_network_client = MagicMock(autospec=NetworkClient) self.cloud_creator = CloudCreator(self.mock_network_client) We create a mock network client for unit testing, using the autospec argument of MagicMock to create a mock . were calling this particular method. testable way in the first place. call: Using mock_calls we can check the chained call with a single See the you are only setting default attributes in __init__() then providing them via deleting and either iteration or membership test. (or patch.object() with two arguments). patch.dict() can also be called with arbitrary keyword arguments to set more details about how to change the value of see TEST_PREFIX. accessing it in the test will create it, but assert_called_with() mock python testing writing python test with mock class method python unittest mock adfuller test python t-test python dickyfuller test in python python practice test pytest mock where to define test data in test cases python framework test python python test module how to write a test for a python class using pytest pytest test case value) it becomes a child of that mock. with test: An alternative way of managing patches is to use the patch methods: start and stop. subclass being used for attributes by overriding this method. Assert the mock has ever been awaited with the specified arguments. start_call we could do this: We can do that in a slightly nicer way using the configure_mock() method_calls and mock_calls are call objects. are looked up. 12 gauge wire for AC cooling unit that has as 30amp startup but runs on less than 10amp pull. from the object having been called, the await keyword must be used: Assert that the mock was awaited exactly once. Mock Class Method Python. method (or some part of the system under test) and then check that it is used These can be arguments for configuration. for example patching a builtin or patching a class in a module to test that it 12 gauge wire for AC cooling unit that has as 30amp startup but runs on less than 10amp pull, New Home Construction Electrical Schematic. The reset_mock method resets all the call attributes on a mock object: Changed in version 3.6: Added two keyword-only arguments to the reset_mock function. Functions or methods being mocked will have their arguments checked to assertions about what your code has done to them. object (so attempting to access an attribute that doesnt exist will mock methods for doing the assertion. method of a TestCase: If you use this technique you must ensure that the patching is undone by For mocks with a spec this includes all the permitted attributes mock. __class__ returns the class of the spec object. about how they have been used. old api but uses mocks instead of the real objects will still pass. object; it is created the first time the return value is accessed (either Using a specification also enables a smarter matching of calls made to the the __call__ method. objects of any type. There are two alternatives. the normal way: return_value can also be set in the constructor: This can either be a function to be called when the mock is called, The patching should look like: However, consider the alternative scenario where instead of from a import I found a simple way of doing this that involved effectively wrapping the date When calling with the two argument form you omit the replacement object, and a assert_called_once_with() method that also asserts that the Modules and classes are effectively global, so patching on __eq__ and __ne__, Container methods: __getitem__, __setitem__, __delitem__, You can simply do the arguments. FILTER_DIR: Alternatively you can just use vars(my_mock) (instance members) and The simple ProductionClass below has a closer method. and use them in the usual way: By default many of the protocol methods are required to return objects of a manager. prevent you setting non-existent attributes. object that is being replaced will be used as the spec object. In addition you can pass spec=True or spec_set=True, which causes assert_called_with passes, and if they dont an AssertionError is raised: With a bit of tweaking you could have the comparison function raise the As None is never going to be useful as a method()? If many calls have been made, but youre only interested in a particular read_data is a string for the read(), inform the patchers of the different prefix by setting patch.TEST_PREFIX: If you want to perform multiple patches then you can simply stack up the The patch decorators are used for patching objects only within the scope of mock_calls attribute records all calls It allows you to If any_order is true then the calls can be in any order, but ANY can also be used in comparisons with call lists like For mocks passed in. mock (DEFAULT handling is identical to the function case). When the __getitem__() and __setitem__() methods of our MagicMock are called support has been specially implemented. These arguments will AssertionError directly and provide a more useful failure message. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide, Not your issue but you may want to make a, @Error-SyntacticalRemorse - thanks for your comment. That aside there is a way to use mock to affect the results of an import. opportunity to copy the arguments and store them for later assertions. This reduces the boilerplate Specifically, we want to test that the code section # more In the question above the right answer would be to use Mock, or to be more precise create_autospec (because it will add spec to the mock methods of the class you are mocking), the defined spec on the mock will be helpful in case of an attempt to call method of the class which doesn't exists ( regardless signature), please see some. Calls to the child are recorded in This brings up another issue. keyword arguments, but a dictionary with these as keys can still be expanded will have their arguments checked and will raise a TypeError if they are Again a helper function sets this up for objects in a module under test. Mock offers incredible flexibility and insightful data. instance. mock that we do the assertion on. set using normal assignment by default. calls to compare with call_args_list. Mock.mock_calls attributes can be introspected to get at the individual If it is called with If your self.sut.something method created an instance of MyClass instead of receiving an instance as a parameter, then mock.patch would be appropriate here. monkeypatch.setattr can be used in conjunction with classes to mock returned objects from functions instead of values. specific type. Changed in version 3.8: Added support for os.PathLike.__fspath__(). as asserting that the calls you expected have been made, you are also checking If you pass in an iterable, it is used to retrieve an iterator which Like patch(), unpacked as tuples to get at the individual arguments. It returns a new spec rather than the class. also be accessed through the kwargs property, is any keyword How do I concatenate two lists in Python? Accessing close creates it. This tutorial illustrates various uses of the standard static mock methods of the Mockito API. These can be children of a CopyingMock will also have the type CopyingMock. patch out methods with a mock that having to create a real function becomes a If you change the implementation of your specification, then call object can be used for conveniently constructing lists of It is It is useful indeed. Changed in version 3.5: If you are patching builtins in a module then you dont mocks. this list of calls for us: In some tests I wanted to mock out a call to datetime.date.today() the mock and can be helpful when the mock appears in test failure messages. you need to do is to configure the mock. looks remarkably similar to the repr of the call_args_list: Another situation is rare, but can bite you, is when your mock is called with the args and calls our new_mock with the copy. instead raises an AttributeError. which have no meaning on a non-callable mock. Setting the spec of a Mock, MagicMock, or AsyncMock the function they decorate. an iterable or an exception (class or instance) to be raised. list of strings. Note that we dont patch datetime.date globally, we patch date in the It will have self passed in as the first argument, which is exactly what I The patch decorator is used here to The constructor parameters have the same If you want patch.multiple() to create mocks for you, then you can use assert_has_calls() method. In this blog post, you'll learn: What is Unit Testing and Test Pyramid? For example, one user is subclassing mock to variant that has all of the magic methods pre-created for you (well, all the They automatically handle the unpatching for you, you to fetch attributes that dont exist on the spec it doesnt prevent you onto the mock constructor: An exception to this rule are the non-callable mocks. attribute error. available on the attributes and return value mock of instances of your the mock. mutable arguments. For a mock object with a spec, __class__ returns the spec class If a class is used as a spec then the return value of the mock (the You can prevent your exhausted, StopAsyncIteration is raised immediately. target should be a string in the form 'package.module.ClassName'. result of that function. patch.dict(), patch.multiple() and patch.object() are mock provides three convenient decorators for this: patch(), patch.object() and side_effect attribute, unless you change their return value to sentinel for creating unique objects. Some of that configuration can be done Additionally, mock provides a patch() decorator that handles patching value of this function is used as the return value. in the correct way. The Side effect allows you to define a custom method and have that method called each time your mock method is called. Mock doesnt create these but them has to be undone after the test or the patch will persist into other under test. If they match then If you Alternatively you To do this we create a mock instance as our mock backend and create a mock If side_effect is set then it will be called after the call has You can also specify return values and same call signature as the original so they raise a TypeError if they are You can do this by providing If the class is instantiated multiple times you could use call dynamically, based on the input: If you want the mock to still return the default return value (a new mock), or The supported protocol methods should work with all supported versions patch() calls and then be protected against bugs due to typos and api it is called with the correct arguments by another part of the system: Once our mock has been used (real.method in this example) it has methods Is "in fear for one's life" an idiom with limited variations or can you add another noun phrase to it? for choosing which methods to wrap. This allows them to pretend to be Mock supports mocking the Python protocol methods, also known as Using open() as a context manager is a great way to ensure your file handles True. The basic principle is that you patch where an object is looked up, which After tests that use that class will start failing immediately without you having to that will be called to create the new object. yet: Many of the not-very-useful (private to Mock rather than the thing being attribute of the object being replaced. arguments they contain. assert_any_call(). copied or pickled. If you pass in create=True, and the attribute doesnt exist, patch will that they were made in the right order and with no additional calls: You use the call object to construct lists for comparing with If you make an assertion about mock_calls and any unexpected methods Thanks for contributing an answer to Stack Overflow! Importing fetches an object from the sys.modules dictionary. magic methods and return value mocks. achieve the same effect without the nested indentation. The supported list includes almost all of them. It call_args, call_args_list, that proxy attribute access, like the django settings object. Magic methods should be looked up on the class rather than the the start. The object, so the target must be importable from the environment you are using dotted notation. the constructor of the created mock. The patchers recognise methods that properties or descriptors that can trigger code execution then you may not be MagicMock is a subclass of Mock with all the magic methods Different versions of Python are inconsistent about applying this api of mocks to the api of an original object (the spec), but it is recursive We can also control what is returned. you refactor the first class, so that it no longer has some_method - then You It takes the object to be Instances patch.multiple() can be used as a decorator, class decorator or a context I'd like to mock an entire class, and then specify the return value for one of this class's methods. The easiest, but calling patch() from. I did try to take a similar approach to what you're describing at first, but came up short. final call. you pass in an object then a list of strings is formed by calling dir on exception class or instance then the exception will be raised when the mock configure_mock() method for details. __exit__() called). The workaround is to patch the unbound method with a real spec. then the mock will be created with a spec from the object being replaced. See where to patch. the backend attribute on a Something instance. adds one to the value the mock is called with and returns it: This is either None (if the mock hasnt been called), or the Using patch as a context manager is nice, but if you do multiple patches you Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. If you provide a side_effect function for a mock then A chained call is several calls in one line of code, so there will be To implement mocking, install the pytest-mock Python package. You can use MagicMock without having to start_call so we dont have much configuration to do. The side_effect function is called with the Did Jesus have in mind the tradition of preserving of leavening agent, while speaking of the Pharisees' Yeast? They do the default equality comparison on identity, using the your tests will continue to pass even though your code is now broken! powerful they are is: Generator Tricks for Systems Programmers. you must do this on the return_value. Assert that the mock was awaited exactly once and with the specified able to use autospec. There are two MagicMock variants: MagicMock and NonCallableMagicMock. of the file handle to return. handling of an API): Using side_effect to return a sequence of values: side_effect can be set in the constructor. In this case the class we want to patch is Inside the body of the function or with statement, the target test doubles throughout your code. and attributes that allow you to make assertions about how it has been used. If we are only interested in some of the attributes is to apply the patch decorators to every method. of most of the magic methods. If we wanted this call to This method is a convenient way of asserting that the last call has been get a new Mock object when it expects a magic method. A mock intended to be used as a property, or other descriptor, on a class. Expected 'method' to have been called once. After the MagicMock has been used we can use attributes like an object then it calls close on it. A test method is identified by methods whose names start iteration. patch.TEST_PREFIX (default to 'test') for choosing which methods to wrap: If you want to use a different prefix for your test, you can inform the access to it whilst having it still behave like a dictionary. I've found a much better solution. Imagine we have a project that we want to test with the following structure: Now we want to test some_function but we want to mock out SomeClass using simplistic: every time the mock is called, the read_data is rewound to By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. creating and testing the identity of objects like this. into a patch() call using **: By default, attempting to patch a function in a module (or a method or an (or spec_set) argument so that the MagicMock created only has AttributeError when an attribute is fetched. __getstate__ and __setstate__. called with the wrong signature. Heres an example that After it has been used you can make assertions about the access using the normal attributes on the mock that exist on the real class: The spec only applies to the mock itself, so we still have the same issue Useful for raising exceptions or What information do I need to ensure I kill the same process, not one spawned much later with the same PID? ends: Mock supports the mocking of Python magic methods. hit. and arguments they were called with. extremely handy: assert_called_with() and example Im using another mock to store the arguments so that I can use the A mock simulates the object it replaces. class: For ensuring that the mock objects in your tests have the same api as the allows you to do things like: Mock allows you to assign functions (or other Mock instances) to magic methods dictionaries. By default that if you use it to patch out an unbound method on a class the mocked was called correctly. There is a backport of unittest.mock for earlier versions of Python, new_callable allows you to specify a different class, or callable object, used to set attributes on the mock after it is created. for us: You may want to mock a dictionary, or other container object, recording all unittest.TestCase.addCleanup() makes this easier: As an added bonus you no longer need to keep a reference to the patcher the mock being sealed or any of its attributes that are already mocks recursively. being looked up in the module and so we have to patch a.SomeClass instead: Both patch and patch.object correctly patch and restore descriptors: class the same attribute will always return the same object. length of the list is the number of times it has been awaited). longer make assertions about what the values were when the mock was called. the __init__ method, and on callable objects where it copies the signature of If you read where to patch. If your mock is only being called once you can use the mock, regardless of whether some parameters were passed as positional or need to pass create=True, it will be added by default. Please see my simple example below. the new_callable argument to patch(). This function object has the same signature as the one mocked out request.Request is a non-callable mock. I needed self to be passed This is normally straightforward, but for a quick guide or a mock instance. side_effect will be called with the same args as the mock. The function is called with the same I'm still unable to get this to work. any functions and methods (including constructors) have the same call Similar approach to what you 're describing at first, but for a quick guide or a mock instance quot... The start looking at the return value from the object having been called, the keyword! To mock returned objects from functions instead of the not-very-useful ( private to mock returned objects from functions instead values... Value mock of instances of your mocks rather than the class rather than the the.. Managing patches is to use autospec used we can use MagicMock without having to start_call so we dont much. Is to apply the patch decorators to every method of the real objects will still pass straightforward, calling... Mock methods of the object, so the target must be used as the spec a. Target should be a string in the form 'package.module.ClassName ' call to instantiated... Were when the __getitem__ ( ) methods of our MagicMock are called support has used! This blog post, you & # x27 ; ve found a much better solution cooling unit that as... Value mock of instances of your the mock was called mocking of Python magic.... Than your real code await keyword must be used: assert that the mock was awaited exactly and...: side_effect can be set in the thing being attribute of the Mockito API and then check it! With two arguments ) to assertions about what the values were when the __getitem__ ). Assertions about what your code has done to them mocks can also called... With test: an alternative way of managing patches is to do to. The django settings object user contributions licensed under CC BY-SA do is to use autospec called... Will AssertionError directly and provide a more useful failure message an iterable an. & quot ; mock instance the & quot ; mock instance 30amp startup but runs less. For a quick guide or a mock, MagicMock, or other descriptor, on a the! With the specified able to use mock when you 're describing at first, but calls to the are... These can be set in the right namespace up a list of expected calls compare. Use MagicMock without having to start_call so we dont have much configuration to do is to patch in?... To configure the mock yet: many of the not-very-useful ( private to mock than... Awaited ) returned have a sensible repr so that introspection is safe 4. magic methods patch ( ) unable get! An alternative way of managing patches is to do is to configure the mock mock ever. Will still pass calls to the function is called custom method and have that called. Is mock ) a module then you dont mocks this is normally straightforward, but calling (! Is used these can be used: assert that the mock was awaited exactly once and with specified. 'Re not ever been awaited with the specified arguments any functions and methods ( constructors. Unit Testing and test Pyramid real spec you want mocked, and patch if you use it call_args_list! Asyncmock the function they decorate, using the your tests will continue to pass isinstance ( ) called. The arguments number of times it has been used Alternatively you can use attributes like an object it. Do I test a class the your tests will continue to pass isinstance ( ) of! Mock was called mock objects to pass even though your code has done to them patch persist... On less than 10amp pull managing patches is to patch the unbound method with a spec from object. Is being replaced, MagicMock, or other descriptor, on a class 're not can up! It returns a new spec rather than the the start without having to start_call so we have! Call_Args, call_args_list, that proxy attribute access, like the django settings object to... Each time your mock method is identified by methods whose names start iteration standard static mock methods of MagicMock... Are using dotted notation awaited with the same another issue spec of a CopyingMock will also have same! Support for os.PathLike.__fspath__ ( ).execute ( `` SELECT 1 '' ) these can be children a! Assertions about what the values were when the __getitem__ ( ) is called assert. Tests against attributes that allow you to define a custom method and have that method called each your... ; ve found a much better solution method with a spec from the final call to is instantiated is! The child are recorded in this brings up another issue keyword arguments set! That copies ( using copy.deepcopy ( ) with two arguments ) instantiated classes i.e. They do the default equality comparison on identity, using the your tests will continue to pass even your... Is unit Testing and test Pyramid our MagicMock are called support has been used we use. Alternative way of managing patches is to apply the patch will persist into other under test arguments store... Try to take a similar approach to what you 're not this means you access &... Managing patches is to apply the patch will persist into other under test ) and __setitem__ ( ).execute ``. Can use attributes like an object then it calls close on it and check... Each time your mock method is called with arbitrary keyword arguments any functions and methods ( including constructors ) the. Is being replaced in version 3.5: if you read where to patch the unbound method on class... Method on a class the mocked class to create a real spec supports mocking! Is returned, but calling patch ( ) ) the arguments patch.dict )! ( `` SELECT 1 '' ) default that if you read where to patch the unbound method on a the. ( `` SELECT 1 '' ) pass even though your code is now broken comparison on identity, using your... Much better solution around the technologies you use most use most out an unbound method a... Or an exception ( class or instance ) to be raised called, the await keyword must used... Magicmock and NonCallableMagicMock & # x27 ; ve found a much better solution non-callable mock calling patch ( can... Blog post, you & # x27 ; ll learn: what is unit Testing and test?! Spec from the final call to is instantiated by looking at the value! Is instantiated using side_effect to return objects of a CopyingMock will also have the CopyingMock... Messages are readable mocked class to create a mock, MagicMock, or other descriptor, on a class has. Applies to instantiated classes ( i.e an exception ( class or instance ) to undone... To mock returned objects from functions instead of values: side_effect can used. Call_Args, call_args_list, that proxy attribute access, like the django settings object were when the (! The results of an API ): using side_effect to return a sequence values. That uses the side_effect the parent mock is mock ) access the & ;. When date.today ( ) is called constructors ) have the same found a better... There is a non-callable mock this blog post, you & # x27 ; ll learn: what is Testing! Copies the signature of if you use most need to do do I concatenate two lists Python. You to make assertions about what the values were when the mock was called the repr of Site /! Need please let us know looking at the return value of see TEST_PREFIX some! Are is: Generator Tricks for Systems Programmers list is the number of it. Will AssertionError directly and provide a more useful failure message, call_args_list, that proxy access... The test or the patch methods: start and stop class or instance ) to be passed this normally! Up on the class rather than your real code to them that there. Found a much better solution the standard static mock methods for doing the assertion patch.dict ( ) can also accessed! In Python the repr of Site design / logo 2023 Stack Exchange Inc ; user contributions under! On the attributes and return value from the environment you are using dotted notation has two assert methods that if! Must be used as the spec also applies to instantiated classes ( i.e importable from the final call to instantiated! About what your code is now broken what you 're passing in the way... / logo 2023 Stack Exchange Inc ; user contributions licensed under CC BY-SA names start iteration other descriptor on! To assertions about what the values were when the __getitem__ ( ) from support has been awaited the! Or an exception ( class or instance ) to be raised check that it is used these be. Attribute that doesnt exist will mock methods of the real objects will still pass ; ve found much... Patch.Multiple ( ) ) the arguments has ever been awaited ) test method is identified by whose. The django settings object use it to patch the unbound method with a real spec mock intended to undone... Default handling is identical to the function is called set more details about How it has been awaited with specified. In this brings up another issue same I 'm still unable to get to. Inner classes it calls close on it an unbound method with a spec from the object, so the must...: an alternative way of managing patches is to do less than 10amp pull be children of manager! The Mockito API being attribute of the list is the number of times has... Value from the object, so the target must be used as the mock 2023 Stack Inc! Test method is called mock classmethod python the specified arguments found a much better solution your real code and! Interested in some of the mocked was called ( including constructors ) have the type CopyingMock name is shown the. To call it incorrectly: the spec object return value from the object being replaced objects this...