I have been asked to test the following code, but have no idea about it.
import AWS from 'aws-sdk';
import db from './db';
async function uploadUserInfo(userID: number) {
const user = db.findByPk(userID);
if(!order) throw new Error('User not Found')
const S3 = new AWS.S3();
await S3.putObject({
Bucket: 'users',
Key:userID,
Body: JSON.stringify(user.get())
})
}
As far as I understand the code, it is an asynchronous function to upload user information, validate it, and uses a new instance of a AWS Class to put the userId into the database.
CodePudding user response:
You might need to test it by inputing different values of UserID
. Since I assume no other part of code has been given, you need to test it with varied input values only, and play around with the Error handling
CodePudding user response:
As written, the function won't run as it has what appears to be an undeclared variable (order
). So you can probably test that and return it to the dev as 'broken'.
It's testable assuming your test environment knows the database content, and that the AWS credentials are correct (and presumably not a live/production instance). If this is true, then you can supply known userID values and check that the AWS bucket receives the data.
The function is not well implemented in that its dependencies (db and AWS) are not injectable, and therefore they cannot easily be substituted with mock implementations for testing purposes.
Some testing tools might allow you to intercept the module loading so that you can provide a mock db and AWS implementation, and that would allow you to check that the relevant calls are made with the correct data, based on the userID parameter.
If you have the ability to modify the function so that it is more testible, then if you add the db and AWS instance as parameters, then you can supply mocks, and use those to validate the internal implementation.