简体   繁体   English

摩卡柴期待。 如何验证 class 是否存在?

[英]Mocha Chai Expect. How can i verify that A class is exists?

As the title mentioned.如标题所述。 I would like to ask that how could i write the test code for this scenario?我想问一下,我该如何为这种情况编写测试代码?

Example... i have a class look like this.示例...我有一个 class 看起来像这样。

let ABC = function()
{
    this.title="sample"
};

Question is how do i use chai expect to check for the existences of ABC class?问题是我如何使用 chai expect 来检查 ABC class 的存在?

Does expect chai do something like this?期望 chai 做这样的事情吗? Expect(ABC).to.be.a.class期望(ABC).to.be.a.class

I'm not sure if you had this answered yet but I had the same question and I just found out我不确定你是否已经回答了这个问题,但我有同样的问题,我刚刚发现

  • so after requiring chai and the expect API所以在需要 chai 和期望 API

  • and after requiring the file where your class resides and setting up your test在需要 class 所在的文件并设置测试之后

  • you can ensure that:您可以确保:

      1. it is a function with .to.be.a('function');它是带有.to.be.a('function');
      1. check that it is an instance of your class with expect(className).to.be.an.instance.of(ClassName);检查它是否是您的 class 的实例,带有expect(className).to.be.an.instance.of(ClassName);
     const expect = chai.expect; const Card = require('../src/Card'); describe('Card', function() { it.skip('should be a function', function() { const card = new Card(); expect(Card).to.be.a('function'); }); it.skip('should be an instance of Card', function() { const card = new Card(); expect(card).to.be.an.instanceof(Card); });```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM