2016-02-10 194 views
0

我是angularjs單元測試的新手。我有一個工廠,我試圖窺探茉莉花,我無法弄清楚測試規範的語法。下面是工廠:AngularJS和Jasmine測試工廠

app.factory('assetFactory', function ($http) { 
    var baseAddress = "../api/"; 
    var url = ""; 
    var factory = {}; 

    factory.getAssets = function (term) { 
     url = baseAddress + "asset/search/" + term; 
     return $http.get(url); 
    }; 
    return factory; 
}); 

這裏是我的測試規範,從而未能在預期的語句(錯誤:已被稱爲預計間諜getAssets):

describe('assetFactory', function() { 
    beforeEach(function() { 
     module('fixedAssetApp'); 
    }); 

    beforeEach(inject(function (assetFactory) { 
     spyOn(assetFactory, 'getAssets').and.callThrough(); 
    })); 

    it('should be defined', inject(function (assetFactory) { 
     expect(assetFactory).toBeDefined(); 
    })); 

    it('should have been called, inject(function (assetFactory) { 
     expect(assetFactory.getAssets).toHaveBeenCalled(); 

    })); 
}); 

回答

1

請添加這種變化。

beforeEach(inject(function (assetFactory) { 
     spyOn(assetFactory, 'getAssets').and.callThrough(); 
     assetFactory.getAssets(); 
    })); 

爲了toHaveBeenCalled()返回true,則必須叫你的功能,無論是在beforeEachit塊。

+0

是的!!!謝謝! – steveareeno