eggjs / eggjs/egg

egg-passport-github: 单元测试报 TypeError: this.req.isAuthenticated is not a function

Open
#3,929 2 comments 0 reactions 1 assignee Claimed by @thonatos View on GitHub
Inactive
Dominant language
TypeScript
Stars
19k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

## What happens?

想在 egg-passport-xxx 单元测试里覆盖一个场景:如果 /passport/xxx/callback?code=xxx 被调用了,那么程序会去做一个登录验证。大概是这样写(以egg-passport-github举例说明):

```javascript
it('should authenticate', () => {
const ctx = app.mockContext({});

app
.httpRequest()
.get('/passport/github/callback?code=1234')
.expect(302);

assert(ctx.isAuthenticated());
});
```
看上去意图是正确的,但是跑起来会报错:

```
TypeError: this.req.isAuthenticated is not a function
at Object.isAuthenticated (node_modules/egg-passport/app/extend/context.js:46:21)
at Context.it (test/passport-github.test.js:61:16)
at Test.Runnable.run (node_modules/co-mocha/lib/co-mocha.js:42:16)
```
这是因为用了 mock 的原因导致 request 对象不是真的 reqeust 吗?有没有大佬 **给个测试 ctx.isAuthenticated 的思路?** 多谢!

## 最小可复现仓库
> 请使用 `npm init egg --type=simple bug` 创建,并上传到你的 GitHub 仓库
https://github.com/Jeff-Tian/egg-passport-github

## 复现步骤,错误日志以及相关配置

```
git clone https://github.com/Jeff-Tian/egg-passport-github
cd egg-passport-github
npm i
npm test
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.