Mocking Passport.js (local strategy) in a unit test

谁都会走 提交于 2019-12-20 18:16:11

问题


I am using the local strategy of Passport.js to authenticate users for my API. I am writing tests for this API, and I need a way of mocking Passport.js in the tests to simulate an authenticated/unauthenticated user hitting the API.

What would be the most efficient way of doing this?


回答1:


One possible option: in your test setup, you can passport.use a mock strategy under the local name:

passport.use('local', new MockStrategy());

Implement MockStrategy to pass the request, or whatever your test needs.




回答2:


A bit late, but I did mock passport.js in a project and created a gist with a basic howto. Should work fine (I used passport version ~0.1.16).




回答3:


you can use the http request objects in node and automate rest requests against your API the same way as it would occur in the real world. Look at node supertest module.



来源:https://stackoverflow.com/questions/18452147/mocking-passport-js-local-strategy-in-a-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!