2017-02-27 42 views
0

我使用sequelize ORM與節點 js和努力使用摩卡測試用例編寫創建數據庫新紀錄一個,測試用例獲取執行及以下的代碼,並提出了sequelize不插入到數據庫,(sequelize,的NodeJS,摩卡,柴)

沒有插入到數據庫 我期待在數據庫中的新記錄,但它不是插入

const app = require('../../app'); 
const chai = require('chai'); 
const should = chai.should(); 
const expect = chai.expect(); 
const models = require('../../app/models/index'); 
const User = models.User; 
let assert = chai.assert; 

describe('Model:User - attribute: password_digest - unit tests',() => { 
    it('password_digest should not be empty', (done) => { 
    console.log('>>>1'); 
    console.log(User.create); 
    User.create({ 
     email: '[email protected]', 
     password: 'aa', 
     password_confirmation: 'aa', 
     role_id: 'User', 
     profile_id: 'Consultant' 
    }).then(function(user) { 
     console.log('>>>2'); 
     console.log(user); 
    }); 
    console.log('>>>3'); 
    done(); 
    }); 
}); 


Model:User - attribute: password_digest - unit tests 
test_1   | >>>1 
test_1   | [Function] 
test_1   | >>>3 
test_1   |  Γ£ô password_digest should not be empty 
+1

嘗試通過增加誤差函數中。然後(功能(用戶){},函數(誤差){ 的console.log( '錯誤') })打印錯誤 可以有多種原因。 –

回答

2

你需要調用done()兩個自己的諾言處理,只有有:已達成的分支(then),所以你等待無極告訴摩卡測試結束前解決,同時也捕捉任何錯誤(分支機構catch)。所以,你的代碼可能是:致電User.create所以你不給時間到數據庫執行插入之後

it('password_digest should not be empty', (done) => { 
    User.create({ 
     email: '[email protected]', 
     password: 'aa', 
     password_confirmation: 'aa', 
     role_id: 'User', 
     profile_id: 'Consultant' 
    }) 
    .then(function(user) { 
     // your user assertions 
     console.log(user); 
     done(); 
    }).catch(done); 
    }); 

你測試的當前版本調用done()

同時使用done作爲您的catch處理程序可確保承諾鏈中的任何錯誤都不會阻止您的測試完成,並有助於爲您打印錯誤。用任何值調用done()都會將測試標記爲失敗。

1

你的承諾大概是失敗了。您需要可以檢查你的錯誤日誌,找出這是怎麼回事,或者處理您承諾的失敗案例,喜歡的東西:

describe('Model:User - attribute: password_digest - unit tests',() => { 
    it('password_digest should not be empty', (done) => { 
    console.log('>>>1'); 
    console.log(User.create); 
    User.create({ 
     email: '[email protected]', 
     password: 'aa', 
     password_confirmation: 'aa', 
     role_id: 'User', 
     profile_id: 'Consultant' 
    }).then(function(user) { 
     console.log('>>>2'); 
     console.log(user); 
    }).catch(function(err) { 
     // Ideally, every time you handle the success of a promise 
     // with `then`, you should also handle the possible failure 
     // of it with `catch` 
     console.log('Error inserting user:'); 
     console.log(err); 
    }); 
    console.log('>>>3'); 
    done(); 
    }); 
}); 

然後,與特定錯誤信息更新您的問題,我們可以進一步幫助您。