How to unit test private methods in BDD / TDD?

前端 未结 12 1014
攒了一身酷
攒了一身酷 2020-12-15 22:03

I am trying to program according to Behavior Driven Development, which states that no line of code should be written without writing failing unit test first.

My ques

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-15 22:27

    If a private method method exists, it's there to be used by a public method. Therefore I'd write a test for the public method.

    I write my tests to test the public parts of a class. If the class is well designed then the private parts get tested by default.

    If the private method isn't called from a public method, then why does it exist?

    In your case I'd do the following

    * Write failing test for the public method
    * Write public method that calls the private method that doesn't exist yet(test still fails as your class is incomplete
    * Write the private method
    * Test should now pass
    

提交回复
热议问题