标签: chai

Mocha/Chai期待.没有抓住抛出的错误

我有问题让Chai expect.to.throw在我的node.js应用程序的测试中工作.测试在抛出错误时保持失败,但是如果我在try中包装测试用例并捕获并断言捕获的错误,则它可以工作.

难道expect.to.throw不喜欢的工作,我认为它应该还是什么?

it('should throw an error if you try to get an undefined property', function (done) {
  var params = { a: 'test', b: 'test', c: 'test' };
  var model = new TestModel(MOCK_REQUEST, params);

  // neither of these work
  expect(model.get('z')).to.throw('Property does not exist in model schema.');
  expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.'));

  // this works
  try { 
    model.get('z'); 
  }
  catch(err) {
    expect(err).to.eql(new Error('Property does not exist in model schema.'));
  }

  done();
});
Run Code Online (Sandbox Code Playgroud)

失败:

19 …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js node.js chai

238
推荐指数
5
解决办法
13万
查看次数

chai测试数组相等不能按预期工作

为什么以下失败?

expect([0,0]).to.equal([0,0]);
Run Code Online (Sandbox Code Playgroud)

什么是测试的正确方法?

mocha.js node.js chai

219
推荐指数
2
解决办法
9万
查看次数

在调用异步函数的mocha测试中如何避免超时错误:超过2000ms的超时

在我的节点应用程序中,我使用mocha来测试我的代码.在使用mocha调用许多异步函数时,我收到超时错误(Error: timeout of 2000ms exceeded.).我该如何解决这个问题?

var module = require('../lib/myModule');
var should = require('chai').should();

describe('Testing Module', function() {

    it('Save Data', function(done) {

        this.timeout(15000);

        var data = {
            a: 'aa',
            b: 'bb'
        };

        module.save(data, function(err, res) {
            should.not.exist(err);
            done();
        });

    });


    it('Get Data By Id', function(done) {

        var id = "28ca9";

        module.get(id, function(err, res) {

            console.log(res);
            should.not.exist(err);
            done();
        });

    });

});
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js chai

190
推荐指数
4
解决办法
14万
查看次数

柴的断言,期待和应该有什么区别?

有什么区别assert,expectshould什么时候使用什么?

assert.equal(3, '3', '== coerces values to strings');

var foo = 'bar';

expect(foo).to.equal('bar');

foo.should.equal('bar');
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js chai

147
推荐指数
2
解决办法
4万
查看次数

如何用mocha和chai正确测试承诺?

以下测试表现得很奇怪:

it('Should return the exchange rates for btc_ltc', function(done) {
    var pair = 'btc_ltc';

    shapeshift.getRate(pair)
        .then(function(data){
            expect(data.pair).to.equal(pair);
            expect(data.rate).to.have.length(400);
            done();
        })
        .catch(function(err){
            //this should really be `.catch` for a failed request, but
            //instead it looks like chai is picking this up when a test fails
            done(err);
        })
});
Run Code Online (Sandbox Code Playgroud)

我该如何妥善处理被拒绝的承诺(并对其进行测试)?

我该如何正确处理失败的测试(即:expect(data.rate).to.have.length(400);

这是我正在测试的实现:

var requestp = require('request-promise');
var shapeshift = module.exports = {};
var url = 'http://shapeshift.io';

shapeshift.getRate = function(pair){
    return requestp({
        url: url + '/rate/' + pair,
        json: true
    }); …
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js promise chai

140
推荐指数
3
解决办法
8万
查看次数

NodeJS UnhandledPromiseRejectionWarning

所以,我正在测试一个依赖于事件发射器的组件.为此,我想出了一个使用Promise with Mocha + Chai的解决方案:

it('should transition with the correct event', (done) => {
  const cFSM = new CharacterFSM({}, emitter, transitions);
  let timeout = null;
  let resolved = false;
  new Promise((resolve, reject) => {
    emitter.once('action', resolve);
    emitter.emit('done', {});
    timeout = setTimeout(() => {
      if (!resolved) {
        reject('Timedout!');
      }
      clearTimeout(timeout);
    }, 100);
  }).then((state) => {
    resolved = true;
    assert(state.action === 'DONE', 'should change state');
    done();
  }).catch((error) => {
    assert.isNotOk(error,'Promise error');
    done();
  });
});
Run Code Online (Sandbox Code Playgroud)

在控制台上我得到一个'UnhandledPromiseRejectionWarning',即使拒绝函数被调用,因为它立即显示消息'AssertionError:Promise error'

(node:25754)UnhandledPromiseRejectionWarning:未处理的promise拒绝(拒绝id:2):AssertionError:Promise错误:expect {Object(message,showDiff,...)}是假的1)应该转换为正确的事件

然后,在2秒后我得到了

错误:超过2000毫秒的超时.确保在此测试中调用done()回调.

自从执行catch回调以来,这甚至更奇怪.(我认为由于某种原因,断言失败阻止了其余的执行)

现在有趣的是,如果我注释掉 …

javascript mocha.js node.js promise chai

113
推荐指数
4
解决办法
30万
查看次数

Chai:如何使用'should'语法测试undefined

教程中使用chai测试angularjs应用程序时,我想使用"should"样式为未定义的值添加测试.这失败了:

it ('cannot play outside the board', function() {
  scope.play(10).should.be.undefined;
});
Run Code Online (Sandbox Code Playgroud)

错误"TypeError:无法读取属性'应该'未定义",但测试通过了"expect"样式:

it ('cannot play outside the board', function() {
  chai.expect(scope.play(10)).to.be.undefined;
});
Run Code Online (Sandbox Code Playgroud)

如何让它与"应该"一起工作?

javascript testing angularjs chai

92
推荐指数
6
解决办法
4万
查看次数

Mocha API测试:获取'TypeError:app.address不是函数'

我的问题

我编写了一个非常简单的CRUD API,我最近开始编写一些使用的测试chai,chai-http但是在运行我的测试时遇到了问题$ mocha.

当我运行测试时,我在shell上收到以下错误:

TypeError: app.address is not a function

我的守则

以下是我的一个测试(/tests/server-test.js)的示例:

var chai = require('chai');
var mongoose = require('mongoose');
var chaiHttp = require('chai-http');
var server = require('../server/app'); // my express app
var should = chai.should();
var testUtils = require('./test-utils');

chai.use(chaiHttp);

describe('API Tests', function() {
  before(function() {
    mongoose.createConnection('mongodb://localhost/bot-test', myOptionsObj);
  });

  beforeEach(function(done) {
    // I do stuff like populating db
  });

  afterEach(function(done) {
    // I do stuff like deleting populated db
  }); …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js node.js express chai

88
推荐指数
6
解决办法
5万
查看次数

什么时候应该在酶/反应测试中使用渲染和浅?

在发布这个问题之前,我试图在sqa stackexchange中搜索,但我发现没有关于浅的帖子并在那里渲染,所以我希望有人可以帮助我.

什么时候我应该使用浅和渲染测试反应组件?根据airbnb文档,我对两者的区别提出了一些看法:

  1. 由于浅层是将组件作为一个单元进行测试,因此它应该用于"父"组件.(例如桌子,包装等)

  2. 渲染用于子组件.

我问这个问题的原因是,我很难弄清楚应该使用哪一个(尽管文档说他们非常相似)

那么,我如何知道在特定场景中使用哪一个?

testing meteor chai reactjs enzyme

87
推荐指数
2
解决办法
4万
查看次数

测试Mocha中引发的错误

我希望能找到一些帮助解决这个问题.我正在尝试为我正在编写的应用程序编写测试.我已将问题提炼到以下示例代码中.我想测试一个错误被抛出.我使用Testacular作为测试运行器,mocha作为框架,chai作为断言库.测试运行,但测试失败,因为抛出了错误!任何帮助是极大的赞赏!

function iThrowError() {
    throw new Error("Error thrown");
}

var assert = chai.assert,
    expect = chai.expect;
describe('The app', function() {
    describe('this feature', function() {
        it("is a function", function(){
            assert.throw(iThrowError(), Error, "Error thrown");
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

javascript testing mocha.js chai

73
推荐指数
3
解决办法
8万
查看次数

标签 统计

chai ×10

mocha.js ×8

javascript ×6

node.js ×6

testing ×3

promise ×2

angularjs ×1

enzyme ×1

express ×1

meteor ×1

reactjs ×1