简体   繁体   English

在我的案例中如何解决单元测试问题?

[英]How to fix the unit test issue in my case?

I am trying to unit test two functions codes and keep getting error of undefined object. 我试图对两个函数代码进行单元测试并不断获取未定义对象的错误。

my controller 我的控制器

vm = this;
//always fire first in the app
vm.getCompany = function() {
    api.getCompany(function(res){        
        //do stuff
    })
}

//always fire second in the app
vm.getEmployee = function() {
    api.getEmployee(function(res){
        //do stuff
    })
}

api service api服务

var company;

function getCompany() {
   var company;
   var q = $q.defer();
   var url = ‘something.com’;

   anotherApi.getCompany(url).then(function(comp){
          company = comp;
          q.resolve(company)
    })
}

function getEmployee = function() {
    var name = company.name
    var url = ‘something.com/’ + name;
    var q = $q.defer();
    anotherApi.getEmployee(url).then(function(employee){
          q.resolve(employee)
    })
}

unit test. 单元测试。

beforeEach(function(){
   module(‘myApp);
        inject(function ($injector) {
            $controller = $injector.get('$controller');
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.$new();
            $httpBackend = $injector.get('$httpBackend');
            api = $injector.get('api');
        });

         vm = $controller'myCtrl', {
            $scope : $scope
        });

})

describe (‘test’, function(){
    it(‘should get company’, function(){
         vm.getCompany();
         $httpBackend.flush();
         // stuff and works
    })
    it(‘should get Employee’, function(){
        vm.getEmployee()
        $httpBackend.flush();
        //getting error says 
        //'undefined' is not an object (evaluating 'company.name’)
    })
})

I am getting 'undefined' is not an object (evaluating 'company.name') under getEmployee function in service. 我得到'undefined' is not an object (evaluating 'company.name')服务中getEmployee函数下'undefined' is not an object (evaluating 'company.name')

I have tried many different ways but still not sure how to fix it, can someone help me about it? 我尝试了很多不同的方法,但仍然不确定如何解决它,有人可以帮助我吗? Thanks! 谢谢!

Issue is in your Service. 问题出在您的服务中。 "company" should be the object literal since you access .name over it else it will through an error which you have specified. “company”应该是对象文字,因为你访问.name,否则它将通过你指定的错误。

Try below code: 试试以下代码:

Service 服务

var company = {};

function getCompany() {
    $http.get(url).then(function(comp){
          company = comp;
          return company;
    })
}

function getEmployee = function() {
    var name = company.name
    $http.get(url).then(function(employee){
        // do stuff    
    }
}

It should work. 它应该工作。

What is the expected behavior of the service if getEmployee is called before getCompany is called? 如果在调用getCompany之前调用getEmployee,那么服务的预期行为是什么? You should at least check for company being null before attempting to use it. 在尝试使用公司之前,您至少应检查公司是否为空。 Also, you may want to consider storing the company in a property that you can access in your service. 此外,您可能需要考虑将公司存储在您可以在服务中访问的属性中。 NOTE: I'm prefixing the property name with an underscore just to make a distinction between the public api and this pseudo-private property: 注意:我在属性名称前面加上下划线,只是为了区分public api和这个伪私有属性:

{
    _company: null,
    getCompany: function() {
        var self = this;
        var url = '...';
        return $http.get(url).then(function(comp){
            self._company = comp;
            return self._company;
        });
    },
    getEmployee: function() {
        var self = this;
        if (!self._company) {
            return null; //or throw error or return rejected promise: $q.reject('company is null')
        } else {
            var url = '...';
            var name = self._company.name;
            return http.get(url);
        }
    }
}

Lastly, you can (and should) test your service separately from your controller now. 最后,您现在可以(并且应该)单独从您的控制器测试您的服务。 In your controller test, you can just spyOn your service methods without it calling through to the server. 在您的控制器测试中,您可以在不调用服务器的情况下监视您的服务方法。 And when you test your service, you can just set the service._company to a mock value when testing the getEmployee method. 当您测试服务时,您可以在测试getEmployee方法时将service._company设置为模拟值。

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

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