When tests become hard
A friend contacted me for advice. He was having trouble writing a test for some code. I asked him to send me the code and the test so I could see what was going on. I encounter this problem quite frequently, so I thought I'd post one of my responses... Okay, here is where I think you are going wrong, and when writing tests becomes hard, it’s usually because of this. It’s important to layer your applications. Web/Services/DB – at least conceptually. What you have is a class that does everything. Web and Service. No-one can reuse your file processing logic without using the web, not even tests! A nice way to code would be to: Write a UNIT test that attempts to process a file. It passes in a file X and asserts that the result is Y. It won’t compile because you haven’t written the file processing code yet! Now you write the service which processes the file The test will compile and run now, but probably fail because you forgot something. Now you can fix your service And ...