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:33] – [9. További tesztek és fejlesztés] 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. 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: ' | ||
| + | |||
| + | 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.1728732795.txt.gz · Last modified: 2024/10/12 11:33 by knehez
