facebook / facebook/TestSlide

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

Đang mở
#218 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug
Ngôn ngữ chính
Python
Star
149
Fork
62
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

**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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.