Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. user_test.js
  2. "use strict";
  3.  
  4. //Tarvitaan chai, mocha ja supertest
  5. //npm install -g mocha
  6. //npm install -g chai
  7. //npm install supertest --save-dev
  8. const mongoose = require('mongoose');
  9. mongoose.Promise = global.Promise;
  10. const Schema = mongoose.Schema;
  11. const chai = require('chai');
  12. const expect = chai.expect;
  13.  
  14.  
  15. //Luodaan uusi schema joka hyväksyy nimi objektin
  16. //'usernamename', 'email', 'password' on vaadittu kenttä
  17. const testSchema = new Schema({
  18. username: {type: String, required: true},
  19. email: {type: String, required: true, unique: true},
  20. password: {type: String, required: true}
  21. });
  22.  
  23. const tiedot = mongoose.model('tiedot', testSchema);
  24. describe('Database tests', function() {
  25. it('should open connection to database', function (done) {
  26. //Ennen testauksen aloitusta, luodaan 'hiekkalaatikko' tietokanta yhteys
  27. //Kun yhteys on muodostettu, lähdetään suorittamaan
  28.  
  29. mongoose.connect('mongodb://localhost/testDatabase');
  30. const db = mongoose.connection;
  31. db.on('error', console.error.bind(console, 'connection error'));
  32. db.once('open', function () {
  33.  
  34. done();
  35. });
  36. console.log('Yhdistetty tietokantaan!');
  37. });
  38. });
  39.  
  40. describe('Test database', function() {
  41. //Tallennetaan objektit 'usernamename', 'email', 'password' ja asetetaan niille arvot
  42. //ota huomioon pakollisuudet require: true kaikissa.
  43.  
  44. it('should add new username,email and password to the database for testing', function (done) {
  45. let testInfo = tiedot({
  46. username: 'TestiBotti',
  47. email: 'testi@botti.fi',
  48. password: 'testi1234'
  49. });
  50. testInfo.save();
  51. console.log('tiedot lisätty');
  52. done();
  53. });
  54.  
  55. });
  56.  
  57. it('should NOT save incorrect format to database', function(done){
  58. //Yritetään tallentaa kelpaamatonta infoa, pitäisi heittää error
  59. let wrongSave = tiedot({
  60. notName: 'Not TestiBotti',
  61. notEmail: 'tamaeiole.fi.email',
  62. notPassword: 'tömmöinen. ei voi. olla'
  63. });
  64.  
  65. wrongSave.save(err => {
  66. if(err){return done(); }
  67. throw new Error('Should generate error!');
  68. });
  69. });
  70.  
  71. it('should retrieve data from test database', function(done) {
  72. //Yrittää löytää käyttäjän joka aiemmin luotiin
  73. tiedot.find({username: 'TestiBotti'}, (err, username) => {
  74. if (err) {
  75. throw err;
  76. }
  77. if (username.length === 0) {
  78. throw new error('No Data!');
  79. }
  80.  
  81. });
  82. done();
  83. });
  84.  
  85.  
  86. //Kaikkien testien jälkeen suljetaan yhteys
  87. after(function(done){
  88. mongoose.connection.close(done);
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement