Pau*_*aul 5 unit-testing functional-testing fetch fetch-mock
我正在使用 wheresrhys fetch-mock npm 模块在我的应用程序中运行功能测试。我想用“POST”方法和特定的有效负载来模拟一次提取。
它看起来像这样:
fetchMock.mock({
routes: {
name: 'LoginSuccess',
matcher: "https://myurl",
method: 'POST',
payload: {
params:{User: "ABCDE@gmail.com", Password: "password"}
},
response: {
result:{
message: "Successful login",
credentials: "XXXXXXXXXXXXX"
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
我想检查我的 fetch 的有效负载并相应地给出响应。例如,我可以模拟用户提交错误密码的一次登录,然后他们再次尝试并提交正确的信息并被授予访问权限。相同的 url,不同的负载,不同的响应。
是否有可能做到这一点?我知道可以检查获取的方法,但我想对有效载荷做同样的事情。
或者有没有更好的方法来做到这一点?
我还没有在模块的自述文件或fetch-mock 包的测试部分找到解决方案。
fetchMock.mock({
routes: [{
name: 'LoginSuccess',
matcher: function(url, opts) {
return (url=="https://myurl" && opts && opts.params && opts.params.User=="ABCDE@gmail.com" && opts.params.Password=="password");
},
response: {
result:{
message: "Successful login",
credentials: "XXXXXXXXXXXXX"
}
}
}, {
name: 'LoginFail',
matcher: function(url, opts) {
return (url=="https://myurl" && opts && opts.params && opts.params.User=="ABCDE@gmail.com" && opts.params.Password!=="password");
},
response: {
result:{
message: "Unsuccessful login"
}
}
}]
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8236 次 |
最近记录: |