Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
lotd77756yI tend to write tests before any code.
That way i can drive out a nice API right off the bat. -
@lotd Yup indeed. It is called TDD (Test Driven Development). You first write tests and write code based on those tests. This makes sure that you only write code that you need and that is works as intended. Well.. If you write your tests correctly of course :p But that is one of the many approaches you can do. As an iOS developer I test during development and again after I finished a feature ;)
-
Usually, I design top-down, but code and test bottom-up. Sometimes, tests are too cumbersome when I don't even have the printf debugger. Then I replace tests by reviews and test after completion.
Actually, I mostly even debug by reviews. -
@lotd but what are you going to test if you still have no code?
Excuse my noobness in TDD but I still don't understand it :/ -
lotd77756y@gitpush you have a general idea of what you wanna build.
So you write tests for the code, before you have it.
- make a nice & fluent API, to interact with.
Then you run the test, and let the fails guide you .. :)
Example Unit test.
```
$transaction = $user->cart->add($product)->checkout ();
Payment::assertCharged($product->cost, $user);
$this->assertEmpty($user->cart);
$this->assertDatabaseHas('purchases', [
'user_id' => $user->id,
'product_id' => $product->id,
'transaction_id' => $transaction->id,
'created_at' => now(),
]);
``` -
@lotd thanks man, but looking at your code this means I need to have a user model, and a cart model, and an item model to add to cart, how do I do this if I have not yet written them
-
zer02486y@gitpush well you don't write all the tests at once... Let.s say u want to make a simple api with one dto and crud operations to a db. So you create a test that gets your object init and check it.s props. Then you write your class so the test will pass then you want a DB connection. You will write a test that creates the DB connection and checks for error==null then you write your DB connector class and so on so forth
-
@Brosyl finally I understand lol thanks man, though this is writing tests after code, which is used at what scenarios?
@lotd thanks man 😀 -
@Brosyl so just like everything else, it's used when it should and not like for example, clean coding style it must always be used
Related Rants
Do you test your codes while coding or after completion?
I test while coding.
PS:- I'm web developer.
question
testing