示例#1
0
class TestRequest(unittest.TestCase):
    def setUp(self):
        self.callback = MagicMock()
        self.o = Request(32)
        self.o.set_callback(self.callback)

    def test_init(self):
        assert self.o.id == 32
        assert self.o.callback == self.callback

    def test_respond_with_return(self):
        cb, response = self.o.return_response(value=5)
        assert cb == self.callback
        assert response.to_dict() == Return(id=32, value=5).to_dict()

    def test_respond_with_error(self):
        cb, response = self.o.error_response(
            exception=ValueError("Test Error"))
        assert cb == self.callback
        assert response.to_dict() == Error(id=32, message=ANY).to_dict()
        assert str(response.message) == "Test Error"

    def test_setters(self):
        self.o.set_callback(MagicMock())
        self.o.callback(888)
        self.callback.assert_not_called()
示例#2
0
class TestRequest(unittest.TestCase):
    def setUp(self):
        self.callback = MagicMock()
        self.o = Request(32, self.callback)

    def test_init(self):
        assert self.o.id == 32
        assert self.o.callback == self.callback

    def test_respond_with_return(self):
        cb, response = self.o.return_response(value=5)
        assert cb == self.callback
        assert response == Return(id=32, value=5)

    def test_respond_with_error(self):
        cb, response = self.o.error_response(
            exception=ValueError("Test Error"))
        assert cb == self.callback
        assert response == Error(id=32, message="ValueError: Test Error")

    def test_setters(self):
        self.o.set_id(123)
        assert 123 == self.o.id
        self.o.set_callback(None)
        self.o.callback(888)
        self.callback.assert_not_called()