如何使用Adonis 5(Adonisjs/fold软件包)在测试中使用模拟?
我有一个使用Adonis 5(核心版本5.4.0)构建的API。测试是用Adonis自己的跑步者进行的, japa (版本3.1.1.1.1.1.1.1.1.1.1.1.1 )。
根据 adonis文档没有模拟的数据库。但是,我们实现了一个功能,该功能使用HTTP请求调用第三方API,并且需要为此编写测试。自然,测试实际上不应称呼该第三方API,必须嘲笑。
我很难在测试中设置模拟,因为JAPA似乎没有办法做到这一点,并且文档中没有提及它。我能想到的最好的是看到 adonis 4 href =“ https://github.com/adonisjs/fold” rel =“ nofollow noreferrer”> adonisjs/fold 软件包以构建模拟并尝试执行相同的操作,但没有结果。
这是我尝试使用adonis/fold
版本8.1.9
的示例,但是使用登录方法进行简化:
// in App/Services/AuthService
export default class AuthService {
public static async userLogin(email: string, password: string) {
// Authentication logic
return {
token: 'realGeneratedToken'
}
}
}
// In AuthService.spec.ts
test('It should login user using mock', async (assert) => {
const ioc = new Ioc()
ioc.useProxies(true)
ioc.fake('App/Services/AuthService', () => {
return {
async userLogin(email: string, password: string) {
return {
token: 'mocked token',
}
},
}
})
const { status, body } = await supertest(BASE_URL).post(LOGIN_URL).send({
email: '[email protected]',
password: 'any_password',
})
assert.equal(status, 200)
assert.equal(body.token, 'mocked token')
ioc.restore('App/Services/AuthService')
})
这不起作用。调用了真实方法,但是在测试中使用折叠也没有错误。 有人知道如何在Adonis 5测试中设置模拟吗?使用adonisjs/fold
正确的方法吗?
I have an API built with Adonis 5 (core version 5.4.0). The tests are made with Adonis' own runner, japa (version 3.1.1).
As per the Adonis documentation, I build my tests to interact with a real test database without mocks. However, we implemented a functionality which calls a third party API using HTTP requests and need to write tests for that. Naturally, the tests should not actually call this third party API and must be mocked.
I'm having difficulties setting mocks in the tests as japa does not seem to have a way to do this and there's no mention of it in the documentation. The best I could come up with was seeing that Adonis 4 uses an adonisjs/fold package to build mocks and tried doing the same, but to no results.
Here's an example of what I tried with adonis/fold
version 8.1.9
, but using a login method for simplification:
// in App/Services/AuthService
export default class AuthService {
public static async userLogin(email: string, password: string) {
// Authentication logic
return {
token: 'realGeneratedToken'
}
}
}
// In AuthService.spec.ts
test('It should login user using mock', async (assert) => {
const ioc = new Ioc()
ioc.useProxies(true)
ioc.fake('App/Services/AuthService', () => {
return {
async userLogin(email: string, password: string) {
return {
token: 'mocked token',
}
},
}
})
const { status, body } = await supertest(BASE_URL).post(LOGIN_URL).send({
email: '[email protected]',
password: 'any_password',
})
assert.equal(status, 200)
assert.equal(body.token, 'mocked token')
ioc.restore('App/Services/AuthService')
})
This does not work. The real method is called, but there are no errors by using fold in the test, either.
Does anyone know how to set up mocks in Adonis 5 tests? Is using adonisjs/fold
the correct approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想出了一种用于为运行服务器嘲笑Adonis提供商的方法,我看到您正在尝试击中运行的服务器。在此示例中,我模拟了Redis。
我正在使用开玩笑和打开的API,但我认为其中一些会一样。
I figured out a way to mock Adonis providers for a running server, and I see you are attempting to hit a running server. In this example, I mock Redis.
I'm using Jest and Open API but I think some of this will be the same.