chris
chris

Reputation: 561

MVC3 and TDD pre or post coding

I am still getting to grips with MVC3, and now I am looking into TDD, and the thing that keeps coming up that makes no sense is.

The first step is to quickly add a test, basically just enough code to fail. 

why create a test for your code to just pass.To me it makes much more sense to write my code then test it and see if it fails, and fix any and all bugs with that may occur then.

Upvotes: 1

Views: 88

Answers (2)

Joe Ratzer
Joe Ratzer

Reputation: 18549

Writing the test first makes you start thinking about how the method will pass and fail - you start thinking about the method more deeply.

Otherwise, it's easy to get straight into the method without much thought, leading to methods that aren't so easy to test. It's all too easy to come back to the unit-test later - it often doesn't happen!

Moreover, if you write the method first at what point do you write the test? When you know it passes, when you're "happy" with it... It's a slippery slope to writing code without thinking about testing.

Upvotes: 1

Andrew Barber
Andrew Barber

Reputation: 40149

If you write the code and then write the tests, then you are not doing Test-Driven Development...

That's what TDD stands for; you write your code to enable pre-written tests to pass. If you don't do it that way, you aren't doing TDD.

The idea is that your tests represent your application's requirements. You write those first, just like you would otherwise write your requirements down on paper before you started coding.

This way, you know when all your tests pass, you are done.

Upvotes: 2

Related Questions