Skip to main content

How should I be creating classes when doing TDD



I have started doing TDD and I am unsure if I am doing it correctly. I have made a Question class and a QuestionTest. The Question class has an addAnswer method that takes an instance of the Answer class. Now, should I be creating only the class Answer and use the default constructor. Or should I be making the Answer class and also provide the constructor with parameters?







question.addAnswer(new Answer("Some", "Argument that I know I will use"));







or:







question.addAnswer(new Answer());







It is probably the last one where I write only as much as I need to proceed.


Comments

  1. Bob, I find that TDD works best for me if I do all my planning ahead. This includes figuring out what classes, methods and constructors are required.

    That way, ALL of my tests work, but they fail out of the box. As I complete code, however, these tests start to work. This is a bit tricky, but it does force you to really think about your tests, and make sure they actually cover all the events you wish to test for. Part of this exercise is to determine what data will be available at which time, and which constructor should be called at which time.

    Have fun with TDD, there's no laws, only guidelines, and it's probably best to do what makes sense in your situation. As long as TDD helps you to be clearer of your goals and understanding of how the application will work, I think you are on the right track.

    ReplyDelete
  2. I disagree with Ewald - "current wisdom" says do no planning ahead. Just start by writing the tests. The tests define the behaviour of your classes - the implementation is "irrelevant" (performance etc notwithstanding) as long as the tests pass.

    Initially all your tests will fail. First make them pass (any way you can). Then refactor. This work flow may be summarised with this mantra:


    Red
    Green
    Refactor


    Repeat until you're happy enough to commit your work to the code base.

    Regarding constructors, unless you have final fields, non-default constructors are a convenience, so don't use them in tests - they may not survive refactoring/review.

    ReplyDelete
  3. What I am reading is that you are test driving the creation of the Question class and during that you decide you need to create the Answer class. You want to write as little as possible and defer creating the full constructor.

    You could instead put writing QuestionTest on hold and start writing AnswerTest. Test that you can construct an Answer in the way that is required (do not make a default constructor if an Answer requires those parameters). Test that after construction your Answer behaves as you expect. You could assert that the getters return the right values if it's a dumb data class.

    Then you could return to testing Question and use the full constructor.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex