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:11] – [4. Mocha konfigurálá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 108: | Line 109: | ||
==== 5. Tesztek írása (TDD módszerrel) ==== | ==== 5. Tesztek írása (TDD módszerrel) ==== | ||
- | Most kezdhetjük a TDD folyamatot: először a teszteket írdjuk meg. Például a '' | + | Most kezdhetjük a TDD folyamatot: először a teszteket írdjuk meg. Például a '' |
<sxh javascript> | <sxh javascript> | ||
- | const assert | + | import |
- | const sinon = require(' | + | import |
- | const bcrypt = require(' | + | |
- | const UserRepository | + | |
- | const UserService = require(' | + | |
- | describe(' | + | describe(' |
| | ||
- | let userService; | + | let userRepository; |
- | let userRepositoryStub; | + | |
beforeEach(function() { | beforeEach(function() { | ||
- | | + | |
- | userService = new UserService(new UserRepository()); | + | |
}); | }); | ||
- | | + | |
- | | + | |
+ | assert.strictEqual(user, | ||
}); | }); | ||
- | it(' | + | it(' |
- | | + | |
- | + | await userRepository.saveUser(newUser); | |
- | const result | + | |
- | assert.strictEqual(result.success, false); | + | const foundUser |
- | assert.strictEqual(result.message, 'Email already in use'); | + | assert.strictEqual(foundUser.email, ' |
+ | assert.strictEqual(foundUser.password, 'hashedPassword'); | ||
}); | }); | ||
- | it(' | + | it(' |
- | | + | |
- | | + | const user2 = { email: ' |
- | const bcryptStub | + | |
- | + | await userRepository.saveUser(user1); | |
- | const result | + | |
- | | + | |
- | assert.strictEqual(bcryptStub.calledOnce, true); | + | const foundUser1 |
- | assert.strictEqual(bcryptStub.calledWith('plainPassword'), true); | + | |
- | assert.strictEqual(result.success, true); | + | |
- | assert.strictEqual(result.message, | + | assert.strictEqual(foundUser1.email, |
+ | assert.strictEqual(foundUser2.email, ' | ||
}); | }); | ||
}); | }); | ||
Line 158: | Line 157: | ||
Futtasd a Mocha teszteket, hogy megbizonyosodj arról, hogy a tesztek elbuknak (mivel még nem írtad meg a tényleges implementációt). | Futtasd a Mocha teszteket, hogy megbizonyosodj arról, hogy a tesztek elbuknak (mivel még nem írtad meg a tényleges implementációt). | ||
- | ```bash | + | < |
npm test | npm test | ||
- | ``` | + | </ |
- | Ez a parancs futtatja a `mocha` parancsot, amely végigmegy a `test` mappában lévő teszteken. Mivel a `UserService` és `UserRepository` még nem implementált, | + | Ez a parancs futtatja a '' |
- | ### 7. **Implementáció megírása** | + | {{: |
+ | |||
+ | ==== 7. Implementáció megírása | ||
Most írd meg a tényleges kódot a tesztek sikeressé tételéhez. | Most írd meg a tényleges kódot a tesztek sikeressé tételéhez. | ||
- | #### `userRepository.js`: | + | '' |
- | ```javascript | + | < |
// repositories/ | // repositories/ | ||
Line 191: | Line 192: | ||
} | } | ||
- | module.exports = UserRepository; | + | export default |
- | ``` | + | </ |
- | #### `userService.js`: | + | ==== 8. Tesztek újrafuttatása ==== |
- | ```javascript | + | Most futtassuk újra a teszteket: |
- | // services/ | + | |
- | const bcrypt = require(' | + | < |
+ | npm test | ||
+ | </ | ||
+ | |||
+ | Most a teszteknek sikeresen át kell menniük, mivel az implementáció megfelel a tesztek elvárásainak. | ||
+ | |||
+ | {{: | ||
+ | |||
+ | |||
+ | ==== 9. BDD stílusú tesztek ==== | ||
+ | |||
+ | Hozzuk létre a '' | ||
+ | |||
+ | <sxh javascript> | ||
+ | import { expect } from ' | ||
+ | import sinon from ' | ||
+ | import bcrypt from ' | ||
+ | import UserRepository from ' | ||
+ | import UserService from ' | ||
+ | |||
+ | 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: ' | ||
+ | |||
+ | | ||
+ | |||
+ | expect(result.success).to.be.false; | ||
+ | expect(result.message).to.equal(' | ||
+ | }); | ||
+ | |||
+ | it(' | ||
+ | // Szimuláljuk, | ||
+ | userRepositoryStub.resolves(null); | ||
+ | |||
+ | // Mockoljuk a bcrypt | ||
+ | const bcryptStub | ||
+ | |||
+ | 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 { | class UserService { | ||
Line 206: | Line 289: | ||
} | } | ||
- | // Felhasználó regisztrálása | ||
async registerUser(email, | async registerUser(email, | ||
- | // Ellenőrizzük, | ||
const existingUser = await this.userRepository.findUserByEmail(email); | const existingUser = await this.userRepository.findUserByEmail(email); | ||
if (existingUser) { | if (existingUser) { | ||
Line 214: | Line 295: | ||
} | } | ||
- | // Hash-eljük a jelszót | ||
const hashedPassword = await bcrypt.hash(password, | const hashedPassword = await bcrypt.hash(password, | ||
- | |||
- | // Új felhasználó létrehozása | ||
const newUser = { email, password: hashedPassword }; | const newUser = { email, password: hashedPassword }; | ||
- | |||
- | // Felhasználó mentése | ||
await this.userRepository.saveUser(newUser); | await this.userRepository.saveUser(newUser); | ||
Line 227: | Line 303: | ||
} | } | ||
- | module.exports = UserService; | + | export default |
- | ``` | + | </ |
- | + | ||
- | ### 8. **Tesztek újrafuttatása** | + | |
- | + | ||
- | Most futtasd újra a teszteket: | + | |
- | + | ||
- | ```bash | + | |
- | npm test | + | |
- | ``` | + | |
- | + | ||
- | 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). | + | |
- | 2. **Implementáció írása** (hogy a teszt átmenjen). | + | |
- | 3. **Refaktorálás** (a kód minőségének javítása a teszt sikerességének megtartásával). | + | |
- | + | ||
- | Ezzel a módszerrel mindig biztos lehetsz abban, hogy a kód megfelel a tesztelt követelményeknek. | + |
tanszek/oktatas/tdd_es_bdd.1728731481.txt.gz · Last modified: 2024/10/12 11:11 by knehez