扫一扫分享
使用TypeScript编写的基于JavaScript的基于属性的测试框架(如QuickCheck) ,基于属性的测试框架检查属性的真实性。属性是这样的语句:对于所有(x,y,...),例如precondition(x,y,...)都认为property(x,y,...)为true。
通过以下方式安装模块:
yarn add fast-check --dev或npm install fast-check --save-dev
示例 :
const fc = require('fast-check');
// Code under test
const contains = (text, pattern) => text.indexOf(pattern) >= 0;
// Properties
describe('properties', () => {
// string text always contains itself
it('should always contain itself', () => {
fc.assert(fc.property(fc.string(), text => contains(text, text)));
});
// string a + b + c always contains b, whatever the values of a, b and c
it('should always contain its substrings', () => {
fc.assert(fc.property(fc.string(), fc.string(), fc.string(), (a,b,c) => {
// Alternatively: no return statement and direct usage of expect or assert
return contains(a+b+c, b));
});
});
});
手机预览