Ayyukan gwaji don jifa daidaitattun keɓantacce
Don gwada keɓantawa, za mu iya amfani throw
da tabbacin da Chai. Wannan ikirari yana ba mu damar tantance nau'in keɓancewar da ya kamata a jefa da kowane ƙarin cikakkun bayanai da muke son ingantawa. Ta haɗa wannan ikirari a cikin shari'o'in gwajin mu, za mu iya tabbatar da cewa ayyukanmu sun kasance kamar yadda ake tsammani da kuma kula da yanayin kuskure daidai.
Bari mu yi la'akari da misali inda muke da aikin da ke raba lambobi biyu. Muna son tabbatar da cewa aikin yana jefa keɓantacce yayin rarraba ta sifili. Za mu iya rubuta shari'ar gwaji ta amfani da tabbacin Chai ' throw
don bincika idan aikin ya jefa daidai DivideByZeroError
lokacin da aka raba ta sifili.
const { expect } = require('chai');
function divide(a, b) {
if(b === 0) {
throw new Error('DivideByZeroError');
}
return a / b;
}
describe('divide',() => {
it('should throw DivideByZeroError when dividing by zero',() => {
expect(() => divide(10, 0)).to.throw('DivideByZeroError');
});
it('should return the correct result when dividing two numbers',() => {
expect(divide(10, 5)).to.equal(2);
});
});
A cikin misalin da ke sama, muna amfani da to.throw
ƙwaƙƙwaran don tabbatar da cewa divide
aikin yana jefa a DivideByZeroError
yayin rarraba ta sifili. An nannade ikirari a cikin wani aiki domin ya iya kama keɓanta kuma ya yi abubuwan da suka dace.
Ta haɗa da gwaje-gwaje don ingantacciyar jifa, za mu iya tabbatar da cewa ayyukanmu suna tafiyar da yanayin kuskure daidai da ba da amsa mai ma'ana lokacin da yanayi na bazata ya faru. Wannan yana taimakawa inganta gabaɗaya inganci da amincin lambar mu.
A ƙarshe, ayyukan gwaji waɗanda ke jefa keɓancewa wani muhimmin al'amari ne na gwajin software. Tare da Chai tabbatarwa throw
, za mu iya tabbatar da sauƙin cewa ayyukanmu suna jefa keɓancewar da ake tsammani idan ya cancanta. Ta hanyar haɗa waɗannan gwaje-gwaje a cikin dabarun gwajin mu, za mu iya haɓaka ƙarfin aikace-aikacenmu da samar da ingantaccen ƙwarewar mai amfani.
A cikin labarin na uku na jerin "Node.js, Mocha, da Chai ", za mu bincika yadda ake gwada ayyuka da hanyoyin ta amfani da Chai. Chai babban ɗakin karatu ne mai ƙarfi don gwada ƙima da sakamako a JavaScript lamba.
Gwajin hanyoyin abu da halayensu
Don tabbatar da hanyoyin wani abu, zamu iya amfani da ikirari da aka bayar ta hanyar tsarin gwaji kamar Mocha da Chai. Waɗannan maganganun suna ba mu damar yin ƙwazo game da kaddarorin da halayen abubuwa.
Bari mu yi la'akari da misali inda muke da wani abu da ake kira calculator
tare da hanyoyin haɓakawa, ragi, ninkawa, da rarrabawa. Muna son tabbatar da cewa waɗannan hanyoyin sun dawo da sakamako daidai. Za mu iya rubuta shari'o'in gwaji ta amfani da Chai ƙwaƙƙwaran' don tabbatar da halayen waɗannan hanyoyin.
const { expect } = require('chai');
const calculator = {
add:(a, b) => a + b,
subtract:(a, b) => a- b,
multiply:(a, b) => a * b,
divide:(a, b) => a / b,
};
describe('calculator',() => {
it('should return the correct sum when adding two numbers',() => {
expect(calculator.add(5, 3)).to.equal(8);
});
it('should return the correct difference when subtracting two numbers',() => {
expect(calculator.subtract(5, 3)).to.equal(2);
});
it('should return the correct product when multiplying two numbers',() => {
expect(calculator.multiply(5, 3)).to.equal(15);
});
it('should return the correct quotient when dividing two numbers',() => {
expect(calculator.divide(6, 3)).to.equal(2);
});
});
A cikin misalin da ke sama, muna amfani da tabbacin Chai 's expect
don tabbatar da cewa hanyoyin abun calculator
sun dawo da sakamakon da ake sa ran. Kowace shari'ar gwaji tana mai da hankali kan takamaiman hanya kuma tana bincika idan ta dawo daidai fitarwa don shigarwar da aka bayar.
Ta hanyar gudanar da waɗannan shari'o'in gwaji, za mu iya tabbatar da cewa hanyoyin abu calculator
suna nuna hali kamar yadda ake tsammani da kuma samar da sakamako mai kyau.
Baya ga duba ƙimar dawowar hanyoyin, muna kuma iya amfani da dagewa don tabbatar da wasu kaddarori da halayen abubuwa. Chai yana ba da fa'idodi da yawa waɗanda ke ba mu damar yin furucin iri-iri akan abubuwa, kamar duba ƙimar dukiya, tabbatar da kiran hanyar, da ƙari.
Ta hanyar gwada hanyoyin abu sosai, za mu iya tabbatar da daidaitattun su da amincin su, wanda ke ba da gudummawa ga ɗaukacin ingancin codebase ɗin mu.