tanszek:oktatas:tdd_es_bdd
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tanszek:oktatas:tdd_es_bdd [2024/10/12 11:32] – [8. **Tesztek újrafuttatása] knehez | tanszek:oktatas:tdd_es_bdd [2025/03/03 09:55] (current) – [9. BDD stílusú tesztek] knehez | ||
---|---|---|---|
Line 101: | Line 101: | ||
<sxh json> | <sxh json> | ||
+ | " | ||
" | " | ||
" | " | ||
Line 111: | Line 112: | ||
<sxh javascript> | <sxh javascript> | ||
- | const assert | + | import |
- | const UserRepository | + | import |
describe(' | describe(' | ||
Line 161: | Line 162: | ||
Ez a parancs futtatja a '' | Ez a parancs futtatja a '' | ||
+ | |||
+ | {{: | ||
==== 7. Implementáció megírása ==== | ==== 7. Implementáció megírása ==== | ||
Line 168: | Line 171: | ||
'' | '' | ||
- | <sxh javascript>> | + | <sxh javascript> |
// repositories/ | // repositories/ | ||
Line 189: | Line 192: | ||
} | } | ||
- | module.exports = UserRepository; | + | export default |
</ | </ | ||
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. | ||
- | ==== 9. További tesztek és fejlesztés ==== | + | {{: |
- | A TDD ciklusban a következő lépés az újabb tesztek írása új funkciókra, | ||
- | 1. **Teszt írása** (a teszt el fog bukni). | + | ==== 9. BDD stílusú tesztek ==== |
- | 2. **Implementáció írása** (hogy a teszt átmenjen). | + | Hozzuk létre |
- | 3. **Refaktorálás** (a kód minőségének javítása a teszt sikerességének megtartásával). | + | <sxh javascript> |
+ | import { expect } from ' | ||
+ | import sinon from ' | ||
+ | import bcrypt from ' | ||
+ | import UserRepository from '../ | ||
+ | import UserService from ' | ||
- | Ezzel a módszerrel mindig biztosak lehetünk abban, hogy a kód megfelel a tesztelt követelményeknek. | + | describe(' |
+ | |||
+ | let userService; | ||
+ | let userRepositoryStub; | ||
+ | beforeEach(function() { | ||
+ | // Mockoljuk az adatbázis hívásokat | ||
+ | userRepositoryStub = sinon.stub(UserRepository.prototype, | ||
+ | userService = new UserService(new UserRepository()); | ||
+ | }); | ||
+ | |||
+ | afterEach(function() { | ||
+ | // Restore minden stubolt funkciót a tesztek után | ||
+ | sinon.restore(); | ||
+ | }); | ||
+ | |||
+ | it(' | ||
+ | // Szimuláljuk, | ||
+ | userRepositoryStub.resolves({ email: ' | ||
+ | |||
+ | const result = await userService.registerUser(' | ||
+ | |||
+ | expect(result.success).to.be.false; | ||
+ | expect(result.message).to.equal(' | ||
+ | }); | ||
+ | |||
+ | it(' | ||
+ | // Szimuláljuk, | ||
+ | userRepositoryStub.resolves(null); | ||
+ | |||
+ | // Mockoljuk a bcrypt hash funkciót | ||
+ | const bcryptStub = sinon.stub(bcrypt, | ||
+ | |||
+ | const result = await userService.registerUser(' | ||
+ | |||
+ | // Ellenőrizzük, | ||
+ | expect(bcryptStub.calledOnce).to.be.true; | ||
+ | expect(bcryptStub.calledWith(' | ||
+ | expect(result.success).to.be.true; | ||
+ | expect(result.message).to.equal(' | ||
+ | }); | ||
+ | |||
+ | it(' | ||
+ | // Szimuláljuk, | ||
+ | userRepositoryStub.resolves({ email: ' | ||
+ | |||
+ | const bcryptStub = sinon.stub(bcrypt, | ||
+ | |||
+ | const result = await userService.registerUser(' | ||
+ | |||
+ | // Ellenőrizzük, | ||
+ | expect(bcryptStub.called).to.be.false; | ||
+ | expect(result.success).to.be.false; | ||
+ | expect(result.message).to.equal(' | ||
+ | }); | ||
+ | }); | ||
+ | </ | ||
+ | |||
+ | A teszt futtatás természetesen hibát ad. | ||
+ | |||
+ | Hozzuk létre az '' | ||
+ | |||
+ | <sxh javascript> | ||
+ | import bcrypt from ' | ||
+ | |||
+ | class UserService { | ||
+ | constructor(userRepository) { | ||
+ | this.userRepository = userRepository; | ||
+ | } | ||
+ | |||
+ | async registerUser(email, | ||
+ | const existingUser = await this.userRepository.findUserByEmail(email); | ||
+ | if (existingUser) { | ||
+ | return { success: false, message: 'Email already in use' }; | ||
+ | } | ||
+ | |||
+ | const hashedPassword = await bcrypt.hash(password, | ||
+ | const newUser = { email, password: hashedPassword }; | ||
+ | await this.userRepository.saveUser(newUser); | ||
+ | |||
+ | return { success: true, message: 'User registered successfully' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | export default UserService; | ||
+ | </ |
tanszek/oktatas/tdd_es_bdd.1728732772.txt.gz · Last modified: 2024/10/12 11:32 by knehez