我开始编写一些自动化测试(API)
现在我尝试对此端点执行以下操作:
https://dog.ceo/api/breeds/image/random 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
谁能帮我?
thanks
你不需要 chai-string 你可以这样做:
expect(response.body.message).to.be.a('string').and.satisfy(msg => msg.startsWith('https://images.dog.ceo/breeds/'));
甚至可以做正则表达式satisfy
.
或者比这个更好,只需使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)