簡體   English   中英

用摩卡和蔡測試

[英]Testing with mocha and chai

這是我第一次嘗試使用mocha和chai,我創建了這個小的測試文件test.spec.js ,並在其中進行了一些測試。

var expect = require('chai').expect;

var foo = 'bar';

describe('Test', function () {
it('Should exist', function () {
    expect(foo).to.exist;
});
it('Should be a string', function () {
    expect(foo).to.be.a('string');
});
it('Should equal bar', function () {
    expect(foo).to.equal('bar');
});
it('Should be at least 3 chars', function () {
    expect(foo).to.have.length.above(2);
});
});

這是與“主”代碼所在的單獨文件。
但是,如何測試我在腳本文件中編寫的實際相關代碼?

我沒有使用ES6,因此無法導入或要求。 但是我正在使用npm,這就是我需要chai的方式。

所以我的專案看起來像這樣

A folder
 - myscript.js
 - test.spec.js

如何測試myscript?

像這樣分配測試腳本:

var myscript = require('./myscript.js');

這是一個例子:

'use strict'

const expect = require('chai').expect
const myscript = require('./myscript.js')

describe('myscript', function () {
  it('should be a function', function() {
    expect(typeof myscript).to.equal('function')
  })

  it("should return an object with toString method", function() {
    expect(myscript().hasOwnProperty("toString")).to.be.true
  })
}) // etc...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM