Пример #1
0
class DescribeRSpecReturnValues(Spec):
    def before(self):
        self.m = Mock()

    def it_should_return_a_specific_value(self):
        self.m.should_access.msg().once.and_return("foo")
        Value(self.m).invoking.msg().should == "foo"

    def it_should_return_consecutive_values(self):
        self.m.should_access.msg().and_return(0, 1, 2, 3, 4)
        for i in range(5):
            Value(self.m).invoking.msg().should == i

    def it_should_return_from_function(self):
        def func():
            return "bar"

        self.m.should_access.msg().and_return_from_callable(func)
        Value(self.m).invoking.msg().should == "bar"

    def it_should_return_yielded_values(self):
        # note: python yield is different from ruby's yield
        def func():
            yield 1
            yield 2
            yield 3

        self.m.should_access.msg().and_return_from_callable(func)

        for i, j in enumerate(self.m.msg()):
            Value(j).should == i + 1
Пример #2
0
class DescribeRSpecExpectingArguments(Spec):
    def before(self):
        self.m = Mock()

    def it_should_expect_arguments(self):
        self.m.should_access.msg(1, 2, 3)
        self.m.msg(1, 2, 3)

    def it_should_expect_arguments_with_count(self):
        self.m.should_access.msg(1, 2, 3).once
        self.m.msg(1, 2, 3)

    def it_should_expect_no_arguments(self):
        self.m.should_access.msg()
        self.m.msg()

    def it_should_expect_any_arguments(self):
        self.m.should_access.msg(ANYTHING)
        self.m.msg(1, 2, 3, 4, 5, 6)
Пример #3
0
class DescribeRSpecReceiveCounts(Spec):
    def before(self):
        self.m = Mock()

    @fails_verification
    def it_should_fail_receiving_less_than_once(self):
        self.m.should_access.msg().once

    @fails_verification
    def it_should_fail_receiving_more_than_once(self):
        self.m.should_access.msg().once
        self.m.msg()
        self.m.msg()

    def it_should_receive_once(self):
        self.m.should_access.msg().once
        self.m.msg()

    @fails_verification
    def it_should_fail_receiving_less_than_twice(self):
        self.m.should_access.msg().twice

    @fails_verification
    def it_should_fail_receiving_more_than_twice(self):
        self.m.should_access.msg().twice
        self.m.msg()
        self.m.msg()
        self.m.msg()

    def it_should_receive_twice(self):
        self.m.should_access.msg().twice
        self.m.msg()
        self.m.msg()

    @fails_verification
    def it_should_fail_receiving_less_than_10_times(self):
        self.m.should_access.msg().exactly(10).times

    @fails_verification
    def it_should_fail_receiving_more_than_10_times(self):
        self.m.should_access.msg().exactly(10).times
        for i in range(15):
            self.m.msg()

    def it_should_receive_10_times(self):
        self.m.should_access.msg().exactly(10).times
        for i in range(10):
            self.m.msg()

    def it_should_receive_at_least_twice(self):
        self.m.should_access.msg().at_least("twice")
        self.m.msg()
        self.m.msg()

    def it_should_receive_at_least_once(self):
        self.m.should_access.msg().at_least("once")
        self.m.msg()

    def it_should_receive_at_least_once_when_called_more_than_once(self):
        self.m.should_access.msg().at_least("once")
        for i in range(5):
            self.m.msg()

    @fails_verification
    def it_should_fail_when_receiving_at_least_once(self):
        self.m.should_access.msg().at_least("once")

    def it_should_receive_at_most_twice_when_called_to_zero(self):
        self.m.should_access.msg().at_most("twice")
        self.m.msg()
        self.m.msg()

    def it_should_receive_at_most_twice(self):
        self.m.should_access.msg().at_most("twice")
        self.m.msg()
        self.m.msg()

    @fails_verification
    def it_should_fail_when_receiving_at_most_twice_when_called_more_than_twice(self):
        self.m.should_access.msg().at_most("twice")
        for i in range(5):
            self.m.msg()

    def it_should_explicitly_expect_imprecise_counts(self):
        self.m.should_access.msg().any_number_of_times

    def it_should_implicitly_expect_at_least_one(self):
        self.m.should_access.msg()
        self.m.msg()
Пример #4
0
class DescribeRSpecArgumentConstraints(Spec):
    def before(self):
        self.m = Mock()

    def it_should_expect_any_argument(self):
        self.m.should_access.msg(1, ANY_ARG, "A")
        self.m.msg(1, "foo", "A")

    def it_should_expect_an_instance_of(self):
        self.m.should_access.msg("a", an_instance_of(int), "b")
        self.m.msg("a", 9, "b")

    def it_should_expect_hash_including(self):
        self.m.should_access.msg("a", "b", dict_includes({"c": "d"}))
        self.m.msg("a", "b", {"c": "d", "e": "f"})

    @fails_verification
    def it_should_fail_when_expecting_hash_including(self):
        self.m.should_access.msg("a", "b", dict_includes({"c": "d"})).once
        self.m.msg("a", "b", {"e": "f"})

    def it_should_expect_bool(self):
        self.m.should_access.msg("a", boolean, "b")
        self.m.msg("a", True, "b")

    def it_should_expect_obj_that_responds_to(self):
        self.m.should_access.msg("a", duck_type("__abs__", "__div__"), "b")
        self.m.msg("a", 5, "b")