Okay, so I'm really struggling to grasp the concept of testing.
My big problem is that whenever people try to explain it, the examples they give are extremely irrelevant and never used in real life.
Testing a simple "A plus B" function is easy, a quick google search will give you 100% working code in less than 30s. Everyone knows that. Easy.
Now, let's go back to the real world for a minute, and take a look at this piece of Node.JS code:
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const cors = require('cors')
const user = require('./routes/user');
const post = require('./routes/post');
mongoose.connect('mongodb://localhost:27017/auth', { useNewUrlParser: true }).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database'+ err)}
);
const app = express();
app.use(cors());
app.use(passport.initialize());
require('./passport')(passport);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/api/user/', user);
app.use('/api/post/', post);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server is running on PORT ${PORT}`);
});
There you go, that's real code from the real world that does real things.
How on earth do I test this? What do I divide? How?
To give you a bit of context, I showed this code to my teacher (I'm a Computer Science student) and after 5 seconds he literally said that this is crap. I don't even remember what he said nor what I should change to the code because, according to him, my code was so shitty I needed much more things. He gave me tons of info in less than 2mn, I forgot 90% of it.
I am trying to follow the so-called Test-Driven Development approach, but again, doing it for stupid simple A plus B functions that no one never uses, yes, I can do. There aren't any functions like that in my code.
Please, help a student who loves programming and would like to understand how to program the right way.
Aucun commentaire:
Enregistrer un commentaire