鍍金池/ 問(wèn)答/HTML/ eggjs做單元測(cè)試時(shí)session問(wèn)題

eggjs做單元測(cè)試時(shí)session問(wèn)題

在eggjs中寫單元測(cè)試代碼如下`class HomeController extends Controller {
async index() {

this.ctx.session = { data: 'coook-test ----------------'} 
console.log(this.ctx.session);
this.ctx.body = 'hi, egg';

}
async getSession() {

console.log(this.ctx.session);
this.ctx.body = 'hi, egg'

}

}`
home.test.js中
` it('should GET /', () => {

const result = app.httpRequest(app)
  .get('/')
  .expect('hi, egg')
  .expect(200)
  .end((err, res) => {
    console.log(res.text);
  });    

});
it('should GET /getSession', () => {

const result = app.httpRequest(app)
  .get('/getSession')
  .expect('hi, egg')
  .expect(200)
  .end((err, res) => {
    console.log(res.text);
  });

});`
測(cè)試在controller中的console.log(this.ctx.session)為空
想請(qǐng)問(wèn)下在egg測(cè)試中怎么去保存session會(huì)話,找了很久還是沒(méi)找到
求幫助……

回答
編輯回答
蟲児飛
app.mockSession({
  foo: 'bar'
});
const ctx = app.mockContext();
console.log(ctx.session.foo);

egg-mock#appmocksessiondata

2017年11月17日 02:17