2016-03-02 24 views
2

我是新來測試。最近我開發了一個Sails項目。它工作正常。但是現在我想爲整個項目編寫測試用例。我面臨的問題是如何首先進行身份驗證,然後使用Mocha和Unit Js測試Sails項目。如何使用Mocha和Unitjs來測試需要登錄的Sails js控制器?

我的一個控制器有一個顯示報告的功能,但只顯示那些登錄的用戶。我該如何編寫這種邏輯的測試用例?

感謝

+0

試試這個, VAR請求=需要( 'supertest'); var cookie; 請求(應用程序) .POST( '/登錄') 。發送({電子郵件: 「[email protected]」,密碼: '密碼'}) .END(功能(ERR,RES){ 水庫should.have.status(200); cookie = res.headers ['set-cookie']; done(); }); // // 和使用(應用程序) 獲得( '/ V1 /你/路徑') .SET( '餅乾',餅乾) .END(功能上的下一個請求 請求的cookie(ERR ,res){res.should.have.status(200); done(); }); 我發現它感謝 –

回答

1

試試這個,

var request=require('supertest'); 
    var cookie; 
    request(app) 
    .post('/login') 
    .send({ email: "[email protected]", password:'password' }) 
    .end(function(err,res){ 
    res.should.have.status(200); 
    cookie = res.headers['set-cookie']; 
    done();   
    }); 

    // 
    // and use the cookie on the next request 
    request(app) 
    .get('/v1/your/path') 
    .set('cookie', cookie) 
    .end(function(err,res){ 
    res.should.have.status(200); 
    done();   
    }); 

感謝

1

在測試環境您可以使用Cookie來保留會話信息。您可以檢查了Cookie的使用機智帆,摩卡和supertest我GIST例如: gist

相關問題