【问题标题】:mocha hooks, only methods not working摩卡钩,只有方法不起作用
【发布时间】:2013-09-22 10:44:20
【问题描述】:

before(), after(), beforeEach(), afterEach() 这样的 mocha 钩子不起作用。 only 方法也不起作用。没有一个 beforeEach 被调用。我收到一个错误has no method 'only'。下面是代码。

describe('Array', function(){
    beforeEach(function(){
      console.log('before every test')
    })
  describe.only('#indexOf()', function(){
    beforeEach(function(){
      console.log('before every test')
    })
    it.only('should return -1 unless present', function(){
        assert.equal(1,2)
    })
    it('should return the index when present', function(){
        assert.equal(1,2);
    })
  })
})

beforeEach(function(){
  console.log('before every test')
})

【问题讨论】:

    标签: mocha.js


    【解决方案1】:

    您的代码对我有用。您需要添加:

    var assert = require('assert');
    

    在顶部。而且,请确保您npm install -g mocha

    然后,我得到:

    $ mocha -R spec temp.js
    
    
      Array
        #indexOf()
          ◦ should return -1 unless present: before every test
    before every test
    before every test
          1) should return -1 unless present
          ◦ should return the index when present: before every test
    before every test
    before every test
          2) should return the index when present
    
    
      0 passing (16ms)
      2 failing
    
      1) Array #indexOf() should return -1 unless present:
         AssertionError: 1 == 2
          at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:12:16)
          at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
          at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
          at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
          at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
          at processImmediate [as _immediateCallback] (timers.js:330:15)
    
      2) Array #indexOf() should return the index when present:
         AssertionError: 1 == 2
          at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:15:16)
          at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
          at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
          at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
          at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
          at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
          at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
          at processImmediate [as _immediateCallback] (timers.js:330:15)
    

    请注意,您的 it.only 将被忽略,因为首先遇到的是 describe.only。如果您无法复制此输出,请报告您使用的节点和 npm 版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多