facebook / facebook/TestSlide

I'm unable to mock inherited methods in a subclass when the parent is also mocked

Open
#218 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
149
Forks
62
PR merge metrics
No merged PRs in 30d

Description

**I'm using `TestSlide` version:** 2.5.7

**Given:** https://gist.github.com/nsheridan/a08dc79a84cc2974710b7a02b78ff77b

**When I run:**

```
% testslide test.py
```

**I expected this to happen:**
The test `test_parent_and_child_no_override` should pass

**But, instead this happened:**
When both the child and parent classes are mocked I get a `NonExistentAttribute` exception when I attempt to mock a non-overridden method in the child class:
```
% testslide test.py
test.TestThing
test_child
test_parent
test_parent_and_child_no_override: NonExistentAttribute: 'a_method' can not be set.
test_parent_and_child_no_override_careful_ordering
test_parent_and_child_override

Failures:

1) test.TestThing: test_parent_and_child_no_override
1) NonExistentAttribute: 'a_method' can not be set.
template class does not have this attribute so the mock can not have it as well.
See also: 'runtime_attrs' at StrictMock.__init__.
File "/Users/nsheridan/homebrew/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/contextlib.py", line 119, in __exit__
next(self.gen)
File "/Users/nsheridan/homebrew/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
yield
File "/Users/nsheridan/homebrew/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 628, in run
testMethod()
File "test.py", line 38, in test_parent_and_child_no_override
self.mock_callable(achild, "a_method").to_return_value(None)

Finished 5 example(s) in 0.2s: .
Successful: 4
Failed: 1
```
As you can see from the example, it is possible to
* mock a method in a child class before the parent is mocked
* mock a method in a child class after the parent is mocked *if* the method is overridden in the child

However if the parent and child classes are both mocked it is not possible to mock non-overridden methods in the child class

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.