I have a repository function on my repository layer. I use sequelize as data access. I will write tests for my function.
Here is the logic I want in English:
- My function should save a user to database by using the email and password
- My function should return an object representation of my created user.
For the logic I write the code below.
const UserModel = require('../models/user'); exports.create = async (email, password) => { const createdUser = await UserModel.create({ email, password }); return createdUser.toJSON(); };
So, I am planning to write integration tests. I will write some tests as below.
const userRepository = require('./repositories/user'); const userModel = require('./model/user'); const chai = require('chai'); it('should create a user by using the correct email and password', async () => { const email = '[email protected]'; const password = 'test@Pass123'; const createdUser = await userRepository.create(email, password); const userInDb = userModel.findOne( { id = createdUser.id } ); chai.expect(userInDb.email).to.be.equal(email); chai.expect(userInDb.password).to.be.equal(password); }); it('should return object representation of created user', async () => { const email = '[email protected]'; const password = 'test@Pass123'; const createdUser = await userRepository.create(email, password); chai.expect(typeof userInDb).to.be.equal('object'); });
So I have several questions.
1- Isn't the test enough? Should I write unit test for it? Or what are the benefits of having unit test for my method with or without my integration test.
2- Should I write integration test and unit test together?
3- How can I write unit test for my method?
Also something seems incorrect to me.
4- My tests are depend on eachother. Like, if my should return object test fails my other test could fail. Because i depent my return result will be an object in first test.
5- My tests depend on the package I use. My repository method uses sequelize for the logic. However if I change sequelize ORM with another ORM. I have to change my tests as well.