什么以及如何为角工厂进行单元测试

时间:2016-02-05 17:05:28

标签: angularjs unit-testing jasmine karma-jasmine

Angular工厂很棒!!在编写单元测试时,如果我们编写单元测试,它会有点混乱。

我有以下工厂:

(function(myApp) {
myApp.factory('MyFirstFactory', function(MySecondFactory){
  function MyFirstFactory(config){
    this.value1 = 'value1';
    setDefault(this);
  }

   MyFirstFactory.doSomething = function(){
     var config = {
       findWork: true;
       myWork: MySecondFactory.doWork()
     };
     return new MyFirstFactory(config);
   };

  return MyFirstFactory;
});
})(angular.module('MyModule'));

因此,对于上述工厂,我们是否需要为MyFirstFacotry和doSomething函数编写单元测试? 如果是的话,我们怎样才能实现使用茉莉和业力。试过以下:

describe('MyFirstFactory', function() {
   var mockMyFirstFactory;
   beforeEach(function(){
     module('MyModule');
     inject(function(MyFirstFactory){
        mockMyFirstFactory = MyFirstFactory;
     });
   });

  it('MyFirstFactory should be defined', function(){
     expect(mockMyFirstFactory).toBeDefined();
  });
  it('should do something', function(){
    // how to test doSomething 
  });
});

我正在使用茉莉,角嘲,业力。

1 个答案:

答案 0 :(得分:2)

与所有单元测试一样,您通过测试仔细检查代码的行为。因此,如果您在致电doSomething()后期望发生某些事情,那么您应该进行测试以反映这种期望。

  it('should do something', function(){
    mockMyFirstFactory.doSomething(thing);
    expect(mockMyFirstFactory.somethingBool).toBe(true);
    expect(mockMyFirstFactory.somethingDone).toBe("some value");
    expect(mockMyFirstFactory.someMethod).toHaveBeenCalled();
  });

以下是我们其中一个工厂样本的片段,我们为简单的常规设置和测试行为构建了测试。

    describe('imageUploader.imageModel module', function () {
        // VARIABLES
        var imageModel, imageModelFactory, $rootScope, sharedDataService;
        // INJECT AND SETUP DEPENDENCIES
        beforeEach(module('imageModel'));

        beforeEach(inject(function (_$rootScope_, $injector, _imageModelFactory_) {
            $rootScope = _$rootScope_;
            sharedDataService = $injector.get('sharedDataService');
            imageModelFactory = _imageModelFactory_;
            spyOn($rootScope, '$apply');
        }));
        // SET UP MOCK DATA
        beforeEach(function () {
            sharedDataService.metadata = [{metadataItemOne: "one", metadataItemTwo: "two"}];
            var loadedData = {
                name: 'test_name.jpg',
                target: {
                    result: 'fakedata12345'
                }
            };
            var fake_file = {
                data: "fakedata12345",
                fileName: loadedData.name,
                fileType: "image",
                fileSize: 1024,
                fileDate: "7-7-2015"
            };
            imageModel = imageModelFactory(fake_file, loadedData);
        });
        // TEST BEHAVIOR
        it('can get an instance of imageModelFactory', function () {
            expect(imageModelFactory).toBeDefined();
        });
        it('can instantiate an instance of imageModel', function () {
            expect(imageModel).toBeDefined();
        });
        it('can set upload state', function () {
            imageModel.setUploaded(true);
            expect(imageModel.isUploaded).toBe(true);
            expect(sharedDataService.imagesSucceeded).toBe(1);
            imageModel.setUploaded(false);
            expect(imageModel.isUploaded).toBe(false);
            expect(sharedDataService.imagesSucceeded).toBe(0);
            expect(imageModel.RelatedToUpload).toHaveBeenCalled()
        });
    }

如果您想要弄清楚如何使用Jasmine来解决这个问题,那么有很多文档和示例可以帮助您自己开始,例如Jasmine Spies