I'm creating a API test framework and using faker to create fake users to post to the API.
I'm testing two endpoints, create user and create reminder, I POST data to both of these.
To create a user I need to post a name, email and password to the user endpoint and I get back a valid JWT.
To create an reminder I need a valid JWT that I post in the header of the create reminder call.
I have a helper function that creates a user json object for me, and I use this in both the create user and create reminder endpoint.
If I run each test on its own the test will pass. However, if I try and run my full suite of tests, my validation fails on the create user endpoint because the email already exists, it was created when I called the create reminder (because this also calls the create user to get the valid token)
Here is my test to create a valid user -
const validUser = require('../data/userData/validUser');
describe('Functional Testing', () => {
describe('Users', () => {
it('It should create a user', async () => {
const response = await usersService.createUser(validUser);
expect(response.statusCode).to.equal(200);
expect(response.body).to.not.be.null;
expect(response.body).to.have.deep.property('token');
});
});
});
Here is the reminder test that users the user service for the token
context('Functional Testing', () => {
describe('Reminder', () => {
it.skip('It should create a reminder', async () => {
const createUserResponse = await user.createUser(validUser);
const token = createUserResponse.body.token;
const response = await reminderService.createReminder(
validReminder,
token
);
expect(response.statusCode).to.equal(201);
expect(response.body).to.have.deep.property('success');
expect(response.body).to.deep.include({ success: 'reminder created' });
});
And here is my module that creates the user -
const faker = require('faker');
module.exports = {
name: 'Test User',
email: `${faker.internet.email()}`,
password: '123123'
};
I would have thought that regardless, the createUser module would always create me a random email, but I think the tests might be running to fast for faker to generate a new random email?
How do I get Faker to always generates a random user?
Aucun commentaire:
Enregistrer un commentaire