Friday 24 May 2019

How do you test a function which is composed of other functions and relies on a db connection?

I have written this function:async function ValidateUserExists(username, email){ if(!username || !email) throw new Error('Invalid number of args passed. Please pass username and email'); let taken_valid_username = null; let taken_valid_email = null; if(username){ taken_valid_username = await UsernameExists(username); } if(email){ taken_valid_email = await EmailExists(email); } if(taken_valid_username) return taken_valid_username; if(taken_valid_email) return taken_valid_email; return null; } I have integration tests that prove UsernameExists() and EmailExists() work correctly.It's main purpose is to see if a user already exists in the database. I can't just mock something here because both UsernameExists() and EmailExists() are designed to reach into the database and see if a user actually does exist.​I have a lot of functions like this as I'm building an app whose main purpose is to manipulate a database, meaning I have A LOT of integration testing happening.​So far the test I've written looks like this:it('should see if a user already exists on an existent email', async ()=>{ const test = await CreateDummyUser(); const user = await ValidateUserExists(test.username, test.email); //expect user properties here await DestroyDummyUser(test); }) ​From everything I've read unit testing should be much more prevalent than integration tests so I'm convinced I'm doing something incorrectly. BUT, a huge part of my business logic is doing things based on data from the database.I know this is a huge topic. Any recommendations on books or blogs or videos would be really helpful.

Submitted May 24, 2019 at 07:42PM by blindly_running

No comments:

Post a Comment