User Tools

Site Tools


tanszek:oktatas:tdd_es_bdd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tanszek:oktatas:tdd_es_bdd [2024/10/12 11:33] – [9. További tesztek és fejlesztés] kneheztanszek:oktatas:tdd_es_bdd [2025/03/03 09:55] (current) – [9. BDD stílusú tesztek] knehez
Line 101: Line 101:
  
 <sxh json> <sxh json>
 +"type": "module",
 "scripts": { "scripts": {
   "test": "mocha"   "test": "mocha"
Line 111: Line 112:
  
 <sxh javascript> <sxh javascript>
-const assert = require('assert')+import assert from 'assert'; 
-const UserRepository = require('../repositories/userRepository');+import UserRepository from '../repositories/userRepository.js';
  
 describe('UserRepository', function() { describe('UserRepository', function() {
Line 161: Line 162:
  
 Ez a parancs futtatja a ''mocha'' parancsot, amely végigmegy a ''test'' mappában lévő teszteken. Mivel a ''UserRepository'' még nem implementált, a tesztek elbuknak, ami a TDD módszer lényege: először a tesztek buknak el, majd az implementáció következik. Ez a parancs futtatja a ''mocha'' parancsot, amely végigmegy a ''test'' mappában lévő teszteken. Mivel a ''UserRepository'' még nem implementált, a tesztek elbuknak, ami a TDD módszer lényege: először a tesztek buknak el, majd az implementáció következik.
 +
 +{{:tanszek:oktatas:pasted:20241012-113656.png}}
  
 ==== 7. Implementáció megírása ==== ==== 7. Implementáció megírása ====
Line 168: Line 171:
 ''userRepository.js'': ''userRepository.js'':
  
-<sxh javascript>>+<sxh javascript>
 // repositories/userRepository.js // repositories/userRepository.js
  
Line 189: Line 192:
 } }
  
-module.exports = UserRepository;+export default UserRepository;
 </sxh> </sxh>
  
Line 202: Line 205:
 Most a teszteknek sikeresen át kell menniük, mivel az implementáció megfelel a tesztek elvárásainak. Most a teszteknek sikeresen át kell menniük, mivel az implementáció megfelel a tesztek elvárásainak.
  
 +{{:tanszek:oktatas:pasted:20241012-113731.png}}
  
  
 +==== 9. BDD stílusú tesztek ====
 +
 +Hozzuk létre a ''test/userRepository.test.js''-t az alábbi tartalommal.
 +
 +<sxh javascript>
 +import { expect } from 'chai';
 +import sinon from 'sinon';
 +import bcrypt from 'bcrypt';
 +import UserRepository from '../repositories/userRepository.js';
 +import UserService from '../services/userService.js';
 +
 +describe('UserService', function() {
 +  
 +  let userService;
 +  let userRepositoryStub;
 +
 +  beforeEach(function() {
 +    // Mockoljuk az adatbázis hívásokat
 +    userRepositoryStub = sinon.stub(UserRepository.prototype, 'findUserByEmail');
 +    userService = new UserService(new UserRepository());
 +  });
 +
 +  afterEach(function() {
 +    // Restore minden stubolt funkciót a tesztek után
 +    sinon.restore();
 +  });
 +
 +  it('should return an error if the email is already in use', async function() {
 +    // Szimuláljuk, hogy az email már létezik
 +    userRepositoryStub.resolves({ email: 'existing@example.com' });
 +
 +    const result = await userService.registerUser('existing@example.com', 'password123');
 +
 +    expect(result.success).to.be.false;
 +    expect(result.message).to.equal('Email already in use');
 +  });
 +
 +  it('should hash the password and register the user if the email is not in use', async function() {
 +    // Szimuláljuk, hogy az email nem létezik
 +    userRepositoryStub.resolves(null);
 +
 +    // Mockoljuk a bcrypt hash funkciót
 +    const bcryptStub = sinon.stub(bcrypt, 'hash').resolves('hashedPassword');
 +
 +    const result = await userService.registerUser('newuser@example.com', 'plainPassword');
 +
 +    // Ellenőrizzük, hogy a bcrypt hash funkciót hívták
 +    expect(bcryptStub.calledOnce).to.be.true;
 +    expect(bcryptStub.calledWith('plainPassword')).to.be.true;
 +    expect(result.success).to.be.true;
 +    expect(result.message).to.equal('User registered successfully');
 +  });
 +
 +  it('should not call bcrypt hash if the email already exists', async function() {
 +    // Szimuláljuk, hogy az email már létezik
 +    userRepositoryStub.resolves({ email: 'existing@example.com' });
 +
 +    const bcryptStub = sinon.stub(bcrypt, 'hash');
 +
 +    const result = await userService.registerUser('existing@example.com', 'plainPassword');
 +
 +    // Ellenőrizzük, hogy a bcrypt hash nem lett meghívva
 +    expect(bcryptStub.called).to.be.false;
 +    expect(result.success).to.be.false;
 +    expect(result.message).to.equal('Email already in use');
 +  });
 +});
 +</sxh>
 +
 +A teszt futtatás természetesen hibát ad.
 +
 +Hozzuk létre az ''services/userService.js'' implementációt az alábbiak szerint:
 +
 +<sxh javascript>
 +import bcrypt from 'bcrypt';
 +
 +class UserService {
 +    constructor(userRepository) {
 +        this.userRepository = userRepository;
 +    }
 +
 +    async registerUser(email, password) {
 +        const existingUser = await this.userRepository.findUserByEmail(email);
 +        if (existingUser) {
 +            return { success: false, message: 'Email already in use' };
 +        }
 +
 +        const hashedPassword = await bcrypt.hash(password, 10);
 +        const newUser = { email, password: hashedPassword };
 +        await this.userRepository.saveUser(newUser);
 +
 +        return { success: true, message: 'User registered successfully' };
 +    }
 +}
 +
 +export default UserService;  // CommonJS helyett exportáljuk ESM szintaxissal
 +</sxh>
tanszek/oktatas/tdd_es_bdd.1728732795.txt.gz · Last modified: 2024/10/12 11:33 by knehez