【问题标题】:Testing Angular with Mocha returning error使用 Mocha 测试 Angular 返回错误
【发布时间】:2015-02-01 09:35:07
【问题描述】:

我使用

安装了 Mocha 和 Shouldjs
npm install -g mocha

npm install -g should

测试我的 Angular 应用程序。我从here复制了一个测试,内容如下:

describe('addition', function () {
 it('should add 1+1 correctly', function (done) {
   var onePlusOne = 1 + 1;
   onePlusOne.should.equal(2);
   // must call done() so that mocha know that we are... done.
   // Useful for async tests.
   done();
 });
});

但是,当我使用

运行测试时
mocha mytest.js

我收到此错误:

  addition
    1) should add 1+1 correctly


  0 passing (7ms)
  1 failing

  1) addition should add 1+1 correctly:
     TypeError: Cannot call method 'equal' of undefined
      at Context.<anonymous> (C:\wamp\www\myproj\v4\static\src\app\project\overview\overview.spec.js:4:27)
      at Test.Runnable.run (C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runnable.js:218:15)
      at Runner.runTest (C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:374:10)
      at C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:452:12
      at next (C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:299:14)
      at C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:309:7
      at next (C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:248:23)
      at Object._onImmediate (C:\Users\Imray\AppData\Roaming\npm\node_modules\mocha\lib\runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:345:15)

谁能看看如何解决这个问题?

【问题讨论】:

    标签: angularjs testing mocha.js should.js


    【解决方案1】:

    查看他们似乎需要将原语包装在 () 中以使其工作的文档:

    https://github.com/shouldjs/should.js

    var should = require('should');
    
    (5).should.be.exactly(5).and.be.a.Number;
    

    还有:

    Should(5).be.exactly(5)
    

    会工作的。

    所以尝试将您的代码更改为:

    (onePlusOne).should.equal(2);

    【讨论】:

    • 现在我得到了Error: Cannot find module 'should',即使我安装了should
    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 2013-04-11
    • 2018-12-05
    • 2016-06-05
    • 2016-04-22
    • 2013-11-08
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多