MCPcopy
hub / github.com/reflex-dev/reflex / test_state_with_invalid_yield

Function test_state_with_invalid_yield

tests/units/test_state.py:1774–1814  ·  view source on GitHub ↗

Test that an error is thrown when a state yields an invalid value. Args: token: A token. mock_base_state_event_processor: The event processor.

(
    token: str,
    mock_base_state_event_processor: BaseStateEventProcessor,
)

Source from the content-addressed store, hash-verified

1772
1773@pytest.mark.asyncio
1774async def test_state_with_invalid_yield(
1775 token: str,
1776 mock_base_state_event_processor: BaseStateEventProcessor,
1777):
1778 """Test that an error is thrown when a state yields an invalid value.
1779
1780 Args:
1781 token: A token.
1782 mock_base_state_event_processor: The event processor.
1783 """
1784
1785 class StateWithInvalidYield(BaseState):
1786 """A state that yields an invalid value."""
1787
1788 def invalid_handler(self):
1789 """Invalid handler.
1790
1791 Yields:
1792 an invalid value.
1793 """
1794 yield 1
1795
1796 captured_exceptions: list[Exception] = []
1797
1798 def capture_exception(ex: Exception) -> None:
1799 captured_exceptions.append(ex)
1800
1801 mock_base_state_event_processor.backend_exception_handler = capture_exception
1802
1803 event = Event(
1804 name=f"{StateWithInvalidYield.get_full_name()}.invalid_handler",
1805 payload={},
1806 )
1807 async with mock_base_state_event_processor as processor:
1808 await processor.enqueue(token, event)
1809
1810 assert len(captured_exceptions) == 1
1811 assert isinstance(captured_exceptions[0], TypeError)
1812 assert "must only return/yield: None, Events or other EventHandlers" in str(
1813 captured_exceptions[0]
1814 )
1815
1816
1817@pytest.fixture

Callers

nothing calls this directly

Calls 3

EventClass · 0.90
get_full_nameMethod · 0.80
enqueueMethod · 0.45

Tested by

no test coverage detected