【问题标题】:Can `describe` contain a mix of `describe` and `it` in Mocha?`describe` 可以在 Mocha 中包含 `describe` 和 `it` 的混合体吗?
【发布时间】:2018-06-24 07:45:12
【问题描述】:

我在 Mocha 中有这个简单的示例单元测试:

const {Builder, By, Key, until} = require('selenium-webdriver')
const chai = require('chai')

chai.should()

var customer
var gigger
var admin

/* eslint-disable no-unused-expressions */

/* globals describe,it */

describe('start all tests', async function () {
  describe('create customer user', async function () {
    it('creates a user browser', async function () {
      customer = await new Builder().forBrowser('chrome').build()
      customer.should.not.be.null

      this.timeout(5000)

      await customer.get('http://www.google.com/ncr')
      await customer.findElement(By.name('q')).sendKeys('webdriver', Key.RETURN)
      await customer.wait(until.titleIs('webdriver - Google Search'), 1000)
    })
    it('creates a gigger browser', async function () {
      gigger = await new Builder().forBrowser('chrome').build()
      gigger.should.not.be.null
    })

    it('creates an admin browser', async function () {
      admin = await new Builder().forBrowser('chrome').build()
      admin.should.not.be.null
    })
  })

  describe('close it all down', async function () {
    it('close all browsers down', async function () {
      await customer.quit()
      await gigger.quit()
      await admin.quit()
    })
  })
})

一切正常。但!如果我取消评论最后一个describe

//describe('close it all down', async function () {
  it('close all browsers down', async function () {
    await customer.quit()
    await gigger.quit()
    await admin.quit()
  })
//})

实际发生的是close all browsers down 实际上直接运行

这是因为describe 函数必须 包含所有describe 函数,而不是it?如果是这样,则没有记录。

或者,我错过了什么?

附录:事实上,Mocha 实际上是如何工作的?我已经习惯了只是打字,以至于我从来没有完全了解它的实际工作方式。摩卡的describe()到底是​​做什么的?它只是运行回调吗?当另一个嵌套的describe() 被调用时,它如何确定它是一个嵌套的,以及它的父对象是哪一个? “总的来说”,这一切是如何运作的?

【问题讨论】:

    标签: javascript node.js mocha.js


    【解决方案1】:

    您所有的测试都是异步运行的,因此您无法控制它们的执行流程,对于您想要做的事情,您应该使用 after() 参见 mocha hooks

    对于您的其他问题,您应该看到这个thread,它的解释非常好。祝测试愉快!

    【讨论】:

    • 我添加了另一个问题...如果你能回答,请告诉我!
    猜你喜欢
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多