我开始编写一些自动化测试(API)。
现在,我试图对这个端点做如下操作:
https://dog.ceo/api/breeds/image/random
所以我在我的函数中添加了
expect(response.body.message).to.startsWith('https://images.dog.ceo/breeds/'); 在试验开始时:
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
有谁可以帮我?
谢谢
发布于 2018-12-30 16:56:07
你不需要猜字串你就能做到:
expect(response.body.message).to.be.a('string').and.satisfy(msg => msg.startsWith('https://images.dog.ceo/breeds/'));甚至可以在那个satisfy中执行regex。
或者比这个更好,只需使用match
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发布于 2021-04-23 16:01:11
我生活在以下解决方案中,没有任何外部依赖。
expect(result.startsWith("string to match")).to.be.true;发布于 2020-07-22 13:17:49
听起来可能是obvious...but,你安装这个软件包了吗?(npm -i chai-string)
https://stackoverflow.com/questions/53979229
复制相似问题