如何在茉莉花中单元测试以下功能?

时间:2022-08-24 21:55:52
I have a following code:

    angular
        .module('testApp')
        .factory('testDataService', function ($http) {
            function testDataService(){
                var self = this;
                self.test = function(){
                    //do something
                }

                self.test1 = function(){
                    // do something                
}
            }

            return new testDataService();

When I try to write a test case like 

    beforeEach(function(){
        new testDataService();
    });

It gives some error like:

> TypeError: '[object Object]' is not a constructor (evaluating 'new testDataService()')

There are numerous functions such as "test", "test1", etc.. inside testDataService. I am not able to test the remaining functions because the scope of the outer function is unreachable. I am not able to get the instance because of "var self=this" Please help.

testDataService里面有很多函数,比如“test”,“test1”等。我无法测试其余功能,因为外部功能的范围无法访问。由于“var self = this”,我无法获得实例请帮忙。

2 个解决方案

#1


1  

There is no need of new operator to create Object.

创建Object不需要新的运算符。

Please check this one

请检查一下

describe("\n\Testing factory", function () {
        var factTestDataService;

        beforeEach(inject(function (testDataService) {
           factTestDataService= testDataService;
        }))


        it("factTestDataService test to defined",function(){

            expect(factTestDataService.test()).toBeDefined()
        })
        it("factTestDataService test1 to defined",function(){

            expect(factTestDataService.test1()).toBeDefined()
        })

         it("factTestDataService to defined",function(){

            expect(factTestDataService).toBeDefined()
        })          
    });

You need to inject testDataService factory and store it in local var.

您需要注入testDataService工厂并将其存储在本地var中。

Then you can use this one to access the method defined in that factory as you do in angular and can check with different Jasmine test method.

然后您可以使用此方法访问该工厂中定义的方法,就像您在角度中一样,并可以使用不同的Jasmine测试方法进行检查。

#2


0  

Your factory already returns a new instance of testDataService. There's no need for calling new. When you try to call new on an instance it throws the error you see.

您的工厂已经返回了testDataService的新实例。没有必要打电话给新人。当您尝试在实例上调用new时,它会抛出您看到的错误。

#1


1  

There is no need of new operator to create Object.

创建Object不需要新的运算符。

Please check this one

请检查一下

describe("\n\Testing factory", function () {
        var factTestDataService;

        beforeEach(inject(function (testDataService) {
           factTestDataService= testDataService;
        }))


        it("factTestDataService test to defined",function(){

            expect(factTestDataService.test()).toBeDefined()
        })
        it("factTestDataService test1 to defined",function(){

            expect(factTestDataService.test1()).toBeDefined()
        })

         it("factTestDataService to defined",function(){

            expect(factTestDataService).toBeDefined()
        })          
    });

You need to inject testDataService factory and store it in local var.

您需要注入testDataService工厂并将其存储在本地var中。

Then you can use this one to access the method defined in that factory as you do in angular and can check with different Jasmine test method.

然后您可以使用此方法访问该工厂中定义的方法,就像您在角度中一样,并可以使用不同的Jasmine测试方法进行检查。

#2


0  

Your factory already returns a new instance of testDataService. There's no need for calling new. When you try to call new on an instance it throws the error you see.

您的工厂已经返回了testDataService的新实例。没有必要打电话给新人。当您尝试在实例上调用new时,它会抛出您看到的错误。