How to use Jasmine to test if an instance is created?

筅森魡賤 提交于 2019-12-13 02:12:38

问题


Hi Am new to using Jasmine. The issue is as follows: I have a number of modules, managed through RequireJS. Now a module A creates an instance of another module B in it. Is it possible to use Jasmine to test whether an instance of B is being created in A? To convey a clearer idea of the code, we have:

             //In module A 
             define(['B',],function(B){
                 function test(){
                    var newTest = new B();
                 };
                 return {test: test};
              });

Now, how do i use Jasmine to test that module A indeed, creates an instance of module B? Thanks in advance!

Regards


回答1:


Here's one way to check the type of an object in a Jasmine test:

describe('ChocolateFactory', function() {
    it('creates an instance of Chocolate', function() {
        var factory = new ChocolateFactory();
        var chocolate = factory.makeChocolate();
        expect(chocolate instanceof Chocolate).toBe(true);
    });
});


来源:https://stackoverflow.com/questions/23062034/how-to-use-jasmine-to-test-if-an-instance-is-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!