Ƙirƙirar Gwaje-gwaje masu Sauƙi tare Mocha da kuma Chai

Gina gwaji na asali ta amfani Mocha da kuma Chai

Don gina ainihin gwaji ta amfani Mocha da Chai, kuna iya bin waɗannan matakan:

1. Shigar Mocha da Chai: Yi amfani da npm(Node Package Manager) don shigarwa Mocha kuma Chai a cikin aikin Node.js. Gudanar da umarni mai zuwa a cikin kundin aikin ku:

npm install mocha chai --save-dev

2. Ƙirƙirar fayil ɗin gwaji: Ƙirƙiri sabon fayil, misali test.js, kuma shigo da bayanan masu zuwa don amfani Mocha da Chai:

const chai = require('chai');  
const expect = chai.expect;  
  
describe('Example Test Suite',() => {  
  it('should pass the test',() => {  
    expect(2 + 2).to.equal(4);  
  });  
});

3. Guda gwajin: Buɗe tashar kuma gudanar da mocha  umarni don aiwatar da gwaje-gwaje. Idan komai ya tafi daidai, zaku ga sakamakon da aka nuna a cikin tashar.

Wannan gwaji na asali yana amfani Mocha da kuma Chai duba lissafi mai sauƙi. A cikin misalin da ke sama, mun bincika cewa sakamakon aikin 2 + 2 ya kamata ya zama daidai da 4. Idan sakamakon ya yi daidai, gwajin zai wuce.

Ta hanyar ƙarawa describe  da it  toshewa, zaku iya gina ƙarin hadaddun gwaje-gwaje da duba sassa daban-daban na lambar tushe.

Lura cewa zaku iya amfani da wasu hanyoyin tabbatarwa da aka bayar Chai, kamar assert ko should, don gwaji. Takamammen amfani ya dogara da zaɓinku da yadda kuke son tsara lambar gwajin ku.

 

Amfani da ikirari da tambayoyi don tabbatar da sakamakon aiki

Lokacin amfani Mocha da Chai gwaji, zaku iya amfani da ikirari da tambayoyi don bincika sakamakon ayyuka. Ga wasu misalan amfani da ikirari da tambayoyi don duba sakamakon ayyuka:

1. Yi amfani da expect tabbatarwa da to.equal tambayar don bincika sakamakon aikin da ke dawo da takamaiman ƙima:

const result = myFunction();  
expect(result).to.equal(expectedValue);

2. Yi amfani da tabbacin' tsammanin' da to.be.true ko to.be.false tambaya don bincika sakamakon aikin da ke dawo da ƙimar boolean:

const result = myFunction();  
expect(result).to.be.true; // or expect(result).to.be.false;

3. Yi amfani da kalmar 'expect' da to.be.null tambayar ko to.be.undefined don bincika sakamakon aikin da ya dawo maras kyau ko ƙima:

const result = myFunction();  
expect(result).to.be.null; // or expect(result).to.be.undefined;

4. Yi amfani da expect ikirari da to.include tambayar don bincika idan an haɗa ƙima a cikin tsararraki ko kirtani:

const result = myFunction();  
expect(result).to.include(expectedValue);

5. Yi amfani da expect ikirari da to.have.lengthOf tambayar don bincika tsawon tsararru ko kirtani:

const result = myFunction();  
expect(result).to.have.lengthOf(expectedLength);

Waɗannan misalan kaɗan ne daga cikin hanyoyi masu yawa don amfani da ikirari da tambayoyi a ciki Mocha da kuma Chai duba sakamakon ayyuka. Kuna iya keɓancewa da amfani da maganganun da suka dace da tambayoyinku dangane da buƙatun gwajin aikinku.

 

Ƙirƙirar shari'o'in gwaji masu nasara da gazawa

Lokacin rubuta shari'o'in gwaji tare Mocha da Chai, yana da mahimmanci a rufe duka yanayin nasara da gazawa. Anan akwai misalan ƙirƙirar shari'o'in gwaji don duka yanayin nasara da gazawa:

1. Harkar Gwajin Nasara:

describe('myFunction',() => {  
  it('should return the expected result',() => {  
    // Arrange  
    const input = // provide the input for the function  
    const expected = // define the expected result  
  
    // Act  
    const result = myFunction(input);  
  
    // Assert  
    expect(result).to.equal(expected);  
  });  
});

2. Matsalar Gwajin Kasawa:

describe('myFunction',() => {  
  it('should throw an error when invalid input is provided',() => {  
    // Arrange  
    const invalidInput = // provide invalid input for the function  
  
    // Act and Assert  
    expect(() => myFunction(invalidInput)).to.throw(Error);  
  });  
});

A cikin yanayin gwajin nasara, kuna ayyana shigarwar don aikin da sakamakon da ake tsammani. Sa'an nan, kuna kiran aikin tare da shigarwa kuma tabbatar da cewa sakamakon ya dace da ƙimar da ake sa ran.

A cikin yanayin gwajin gazawa, kuna samar da shigarwar da ba daidai ba ga aikin kuma ku tabbatar yana jefa kuskure. Wannan yana tabbatar da cewa aikin yana sarrafa yanayin shigarwa mara inganci ko kuskure.

Ta hanyar rufe yanayin nasara da gazawa a cikin shari'ar gwajin ku, zaku iya tabbatar da cewa an gwada lambar ku sosai kuma tana sarrafa yanayi daban-daban yadda ya kamata.