首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError: expect(.).to.startsWith不是函数- chai和chakram

TypeError: expect(.).to.startsWith不是函数- chai和chakram
EN

Stack Overflow用户
提问于 2018-12-30 16:12:19
回答 4查看 10.3K关注 0票数 23

我开始编写一些自动化测试(API)。

现在,我试图对这个端点做如下操作:

https://dog.ceo/api/breeds/image/random

所以我在我的函数中添加了

代码语言:javascript
复制
expect(response.body.message).to.startsWith('https://images.dog.ceo/breeds/');   

在试验开始时:

代码语言:javascript
复制
    var chakram = require('chakram');
var chai = require('chai');  
chai.use(require('chai-string'))
expect = chai.expect;    // Using Expect style
expect = chakram.expect;

早些时候,我没有任何问题,但这“期待开始.”运行测试后,我得到了: TypeError: expect(.).to.startsWith不是函数- chai和chakram

有谁可以帮我?

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-12-30 16:56:07

你不需要猜字串你就能做到:

代码语言:javascript
复制
expect(response.body.message).to.be.a('string').and.satisfy(msg => msg.startsWith('https://images.dog.ceo/breeds/'));

甚至可以在那个satisfy中执行regex。

或者比这个更好,只需使用match

代码语言:javascript
复制
const { escapeRegExp } = require('lodash');
expect(response.body.message).to.be.a('string').and.match(/^https:\/\/images\.dog\.ceo\/breeds\//i);
expect(response.body.message).to.be.a('string').and.match(new RegExp('^' + escapeRegExp('https://images.dog.ceo/breeds/'), 'i')); // this is preferred way so you don't have to worry about escaping, rely on lodash method to escape
票数 41
EN

Stack Overflow用户

发布于 2021-04-23 16:01:11

我生活在以下解决方案中,没有任何外部依赖。

代码语言:javascript
复制
expect(result.startsWith("string to match")).to.be.true;
票数 15
EN

Stack Overflow用户

发布于 2020-07-22 13:17:49

听起来可能是obvious...but,你安装这个软件包了吗?(npm -i chai-string)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53979229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档