簡體   English   中英

使用Mocha返回錯誤測試Angular

[英]Testing Angular with Mocha returning error

我使用以下方法安裝了Mocha和Shouldjs

npm install -g mocha

npm install -g should

測試我的Angular應用。 我從這里復制了一個測試,內容如下:

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)

誰能看到該如何解決?

看一下文檔,他們似乎需要將原語包裝在()中才能正常工作:

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);

暫無
暫無

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

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