Skip to content

Latest commit

 

History

History
61 lines (49 loc) · 1.27 KB

Session_2.md

File metadata and controls

61 lines (49 loc) · 1.27 KB

Session 2

These are all the examples for the quiz in the second Automated Testing LU session.

Example 1

function validateEmail(input) {
    const parts = input.split('@')
    return parts.length == 2 &&
        parts[0].length > 0 &&
        parts[1].length > 0
}

test('email validation fails on a wrong email', () => {
    // given
    const userInput = 'johnmiller(at)gmail.com'
    // when
    const isValidEmail = validateEmail(userInput)
    // then
    expect(isValidEmail).toBe(false)
})

Example 2

import { db } from './db'
import { saltAndHash } from './security'

function createUser(email, password) {
    const preliminaryUsername = email.split('@')[0]
    return db.createUser({
        name: preliminaryUsername,
        password: saltAndHash(password),
    })
}

test('createUser should extract the username from the email', () => {
    // given
    const expectedUsername = 'john'
    const enteredEmail = expectedUsername + '@example.com'
    const enteredPassword = 'password123'
    // when
    const createdUser = createUser(enteredEmail, enteredPassword)
    // then
    expect(createdUser.name).toBe(expectedUsername)
})

Example 3

class User:
    ...

    def getName(self):
        return self.name

    ...