如何用玩笑测试护照LocalStrategy

时间:2019-09-28 12:47:49

标签: express testing jestjs passport.js

具有快速护照LocalStrategy测试的股票,可能是根据模拟要求提供的。

o

但是从未调用过回调函数,也没有找到密码和用户名用于护照功能。有没有人想过如何使用开玩笑来嘲笑凭证(我认为这是线索)?

test('should Test password Login auth', async (done) => {
  const response = jest.fn((arg)=> console.log('args', arg));
  const next = jest.fn();
  let mockReq = {body: { username: "test@gmail.com", password: "tets"}}
  let mockRes = {send: function(ats){console.log("mockResFunc", ats), response()}};

  passport.authenticate('local', ()=> response())(mockReq, mockRes);

  expect(next).not.toHaveBeenCalled();
  expect(response).toHaveBeenCalled();

1 个答案:

答案 0 :(得分:0)

不要为诸如本地策略之类的东西编写模拟函数,而是编写实际的函数

const request = require('supertest')
const app = require('../server/app')
describe('Login', () => {
  it('should fail with incorrect credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'dummy',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(401)
    expect(res.body).toHaveProperty('message')
  })

  it('should succeed with correct credentials', async () => {
    const res = await request(app)
      .post('/auth/login')
      .send({
        email: 'demo',
        password: 'demo'
      })
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ email: 'demo' })
  })
})

describe('Logout', () => {
  it('should logout successfully', async () => {
    const res = await request(app).post('/auth/logout')
    expect(res.statusCode).toEqual(200)
    expect(res.body).toEqual({ ok: true })
  })
})