“Hello World” - The TDD way?

前端 未结 10 860
感动是毒
感动是毒 2021-01-31 21:10

Well I have been thinking about this for a while, ever since I was introduced to TDD. Which would be the best way to build a \"Hello World\" application ? which would print \"H

10条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-31 21:50

    Pseudo-code:

    • Create a mock of something that accepts a stream.
    • Invoke helloworld onto this mock through some sort of dependency injection (Like a constructor argument).
    • Verify that the "Hello World" string was streamed into your mock.

    In production code, you use the prompt instead of the mock.

    Rule of thumb:

    • Define your success criteria in how the component interacts with other stuff, not just how it interacts with you. TDD focuses on external behavior.
    • Set up the environment (mocks) to handle the chain of events.
    • Run it.
    • Verify.

提交回复
热议问题