Mock out QApplication.allWidgets.
This could return widgets which are still alive from previous tests, so it's not reliable.
This commit is contained in:
parent
5bd55cb38b
commit
bf156cf554
@ -87,11 +87,14 @@ class FakeQApplication:
|
||||
|
||||
"""Stub to insert as QApplication module."""
|
||||
|
||||
def __init__(self, style=None):
|
||||
def __init__(self, style=None, all_widgets=None):
|
||||
self.instance = mock.Mock(return_value=self)
|
||||
|
||||
self.style = mock.Mock(spec=QCommonStyle)
|
||||
self.style().metaObject().className.return_value = style
|
||||
|
||||
self.allWidgets = lambda: all_widgets
|
||||
|
||||
|
||||
class FakeUrl:
|
||||
|
||||
|
@ -216,7 +216,7 @@ class ExampleObject(QObject):
|
||||
return '<ExampleObject {}>'.format(self._num)
|
||||
|
||||
|
||||
class ExampleWidget(QWidget):
|
||||
class ExampleWidget(QObject):
|
||||
|
||||
def __init__(self, num, parent=None):
|
||||
self._num = num
|
||||
@ -226,12 +226,11 @@ class ExampleWidget(QWidget):
|
||||
return '<ExampleWidget {}>'.format(self._num)
|
||||
|
||||
|
||||
def test_get_all_objects(qtbot):
|
||||
def test_get_all_objects(stubs, monkeypatch):
|
||||
# pylint: disable=unused-variable
|
||||
w1 = ExampleWidget(1)
|
||||
qtbot.add_widget(w1)
|
||||
w2 = ExampleWidget(2)
|
||||
qtbot.add_widget(w2)
|
||||
widgets = [ExampleWidget(1), ExampleWidget(2)]
|
||||
app = stubs.FakeQApplication(all_widgets=widgets)
|
||||
monkeypatch.setattr(debug, 'QApplication', app)
|
||||
|
||||
root = QObject()
|
||||
o1 = ExampleObject(1, root)
|
||||
|
Loading…
Reference in New Issue
Block a user