def test_run_shouldRaiseOnException(self):
        # Given
        action = Mock(spec=['execute', 'command'])
        action.command.readable.return_value = "Mocked action command"
        action.execute.side_effect = Exception("This is a mocked exception.")
        sut = ActionBundle(name="A failing Actionbundle", actions=[action])

        # When / Then
        with self.assertRaises(Exception):
            sut.run(browser=self.__nullBrowser)
    def test_run_withZeroActionsShouldDoNothing(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        sut = ActionBundle(name="Actionbundle without actions", actions=[])

        # When
        sut.run(browser=self.__nullBrowser)

        # Then
        action1.execute.assert_not_called()
    def test_run_withNoneActionsShouldDoNothing(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        # noinspection PyTypeChecker
        sut = ActionBundle(name="Actionbundle without actions", actions=None)

        # When
        sut.run(browser=self.__nullBrowser)

        # Then
        action1.execute.assert_not_called()
    def test_run_shouldCallProxyInsteadOfDirectCall(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        sut = ActionBundle(name="A test Actionbundle name", actions=[action1])

        with mock.patch('story.actionBundle.ActionProxy',
                        autospec=True) as proxy:
            # When
            sut.run(browser=self.__nullBrowser)

            # Then
            proxy(action1).execute.assert_called_once()
            action1.execute.assert_not_called()
    def test_run_withValidDataShouldLoopOverActions(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        action2 = Mock(spec=[Action, 'execute'])
        action3 = Mock(spec=[Action, 'execute'])
        sut = ActionBundle(name="A test Actionbundle name",
                           actions=[action1, action2, action3])

        # When
        sut.run(browser=self.__nullBrowser)

        # Then
        action1.execute.assert_called_once()
        action2.execute.assert_called_once()
        action3.execute.assert_called_once()
    def test_run_shouldStopLoopWhenReceivedStopSignal(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        action2 = Mock(spec=[Action, 'execute'])
        sut = ActionBundle(name="A callback Actionbundle",
                           actions=[action1, action2])

        def stopImmediately():
            sut.stop()

        # When
        sut.run(browser=self.__nullBrowser,
                callback=lambda _: stopImmediately())

        # Then
        # Two actions were configuration in this test but we called stop after the
        # first callback and expect the 2nd action not to be called at all.
        action2.execute.assert_not_called()
    def test_run_shouldCallbackIfCallbackMethodIsGiven(self):
        # Given
        action1 = Mock(spec=[Action, 'execute'])
        action2 = Mock(spec=[Action, 'execute'])
        myCallback = Mock(spec=["action"])
        expectedCallbackArgs = [mock.call(action1), mock.call(action2)]
        sut = ActionBundle(name="A callback Actionbundle",
                           actions=[action1, action2])

        # When
        sut.run(browser=self.__nullBrowser,
                callback=lambda action: myCallback(action))

        # Then
        # Two actions were configured in this test, so we expect 2 callbacks
        self.assertEqual(2, myCallback.call_count)
        # Expect that configured test-actions get passed back along with the callback
        self.assertEqual(expectedCallbackArgs, myCallback.call_args_list)