示例#1
0
    def test_called_with(self):
        mock_subject = Mock()

        allow(mock_subject).to(
            be_called_with("thing", eggs="spam").and_return("bar"))

        assert mock_subject("thing", eggs="spam") == "bar"
示例#2
0
    def test_setting_return_value_twice_raises_error(self):
        mock_subject = Mock()

        with pytest.raises(AllowsException):
            allow(mock_subject).to(
                receive_method("foo").and_return_value("bar").and_return_value(
                    "bar2"))
示例#3
0
    def test_can_start_side_effect_grammar_with_raised_exception(self):
        mock_subject = Mock()

        allow(mock_subject).to(raise_exception(ValueError))

        with pytest.raises(ValueError):
            mock_subject()
示例#4
0
    def test_raising_an_error_with_arbirary_args(self):
        mock_subject = Mock()

        allow(mock_subject).to(
            receive_method("bar").and_raise(ValueError("Stuff happened")))

        with pytest.raises(ValueError, match="Stuff happened"):
            mock_subject.bar("stuff", "things")
示例#5
0
    def test_return_value(self):
        mock_subject = Mock()

        allow(mock_subject).to(
            receive_method("process").called_with(
                "stuff", thing="hi").and_return_value("things"))

        mocked_return_value = mock_subject.process("stuff", thing="hi")
        assert mocked_return_value == "things"
示例#6
0
    def test_with_effect(self):
        mock_subject = Mock()

        allow(mock_subject).to(
            receive_method("process").called_with(
                "stuff",
                thing="hey").with_effect(lambda *args, **kwargs: 2 + 3))

        mocked_return_value = mock_subject.process("stuff", thing="hey")
        assert mocked_return_value == 5
示例#7
0
    def test_raises_exception(self):
        mock_subject = Mock()

        allow(mock_subject).to(
            receive_method("process").called_with("badstuff",
                                                  thing="yo").and_raise(
                                                      ValueError("Bad Stuff")))

        with pytest.raises(ValueError):
            mock_subject.process("badstuff", thing="yo")
示例#8
0
    def test_returns_the_specified_values_in_order_then_keeps_returning_the_last_value(
            self):
        mock_subject = Mock()

        allow(mock_subject).to(receive_method("foo").and_return(1, 2, 3))

        assert mock_subject.foo() == 1
        assert mock_subject.foo() == 2
        assert mock_subject.foo() == 3
        assert mock_subject.foo() == 3
        assert mock_subject.foo() == 3
示例#9
0
    def test_can_start_side_effect_grammar_with_return_value(self):
        mock_subject = Mock()

        allow(mock_subject).to(return_value(1).on_method("bar"))

        assert mock_subject.bar() == 1
示例#10
0
    def test_setting_method_twice_raises_error(self):
        mock_subject = Mock()

        with pytest.raises(AllowsException):
            allow(mock_subject).to(receive_method("foo").on_method("foo"))
示例#11
0
    def test_returns_the_specified_return_value(self):
        mock_subject = Mock()

        allow(mock_subject).to(receive_method("foo").and_return_value("bar"))

        assert mock_subject.foo() == "bar"
示例#12
0
    def test_none_is_returned_by_default(self):
        mock_subject = Mock()

        allow(mock_subject).to(receive_method("foo"))

        assert mock_subject.foo() is None