2014-05-19 77 views
1

我有一個基於折扣,數量等來計算產品價格的angularjs服務。我試圖編寫一個茉莉花測試來調用此服務,並傳入測試數據。我收到一個錯誤,表示應用程序缺少依賴關係。我不想加載Ui路由器,不應該嘲笑這件事嗎?設置Jasmine AngularJS服務測試時未知的提供商

Error: [$injector:nomod] Module 'ui.router' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 

這是我的Jasmine SpecRunner.html。我正在測試的Web項目與我的Jasmine測試項目不同。

<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Jasmine Spec Runner v2.0.0</title> 

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css"> 

    <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine.js"></script> 
    <script type="text/javascript" src="lib/jasmine-2.0.0/jasmine-html.js"></script> 
    <script type="text/javascript" src="lib/jasmine-2.0.0/boot.js"></script> 
    <script src="http://localhost:54411/Scripts/vendor/angular.js"></script> 
    <script src="http://localhost:54411/Scripts/vendor/angular-mocks.js"></script> 
    <script src="http://localhost:54411/Scripts/app.js"></script> 

    <!-- include source files here... --> 
    <script src="http://localhost:54411/Scripts/services/productPriceCalculatorSvc.js"></script> 

    <!-- include spec files here... --> 
    <script src="spec/ProductPriceCalculatorSpec.js"></script> 
</head> 

<body> 
</body> 
</html> 

該規範文件:

describe("Product Price Calculator service test", function() { 
    describe("when I call product price calculator.calculateCustomerDiscPrice", function() { 
     var sut; 

     beforeEach(function() { 
      module('quoteMasterApp'); 
      inject(function(productPriceCalculatorSvc) { 
       sut = productPriceCalculatorSvc; 
      }); 
     }); 

     it('can calculate customer discount price', function() { 
      productPriceCalculatorSvc.calculateCustomerDiscPrice(null, null); 
     }); 
    }); 
}); 

這裏是我的服務宣言。

myApp.service("productPriceCalculatorSvc", [ 
    function() { 
     return { 
      calculateCustomerDiscPrice: function(product, conversionRate) { 
       // calculations occur here 
       }); 
      } 
     } 
    } 
]) 

回答

0

您需要告訴框架如何找到您的服務。

喜歡的東西:

describe("ProductPriceCalculator", function() { 
    var productPriceCalculatorSvc; 

    beforeEach(function() { 
     module('productPriceCalculatorSvcFactory'); 
    }); 

    beforeEach(inject(function ($injector) { 
     productPriceCalculatorSvc = $injector.get('productPriceCalculatorSvcFactory'); 
    })); 

    it('can calculate customer discount price', function() { 
     productPriceCalculatorSvc.calculateCustomerDiscPrice(); 
    }); 
}); 

查看更多在這裏:https://docs.angularjs.org/api/auto/service/ $注射器

+0

什麼是您正在引用的productPriceCalculatorSvcFactory?我的應用程序不使用工廠的服務。 –

+0

這應該很容易。沒有依賴關係的單一服務。在4小時內都沒有取得進展:(我還以爲angularjs被假設是可測試 –

+0

有沒有遇到什麼成功 – shortfinals

0

我有同樣的情況:測試控制器,調用服務。
以下是我在我的粗糙地

describe('Controller Test', function() { 

    var scope, ctrl; 
    // load your controllers including myController 
    beforeEach(module('mycontrollers')); 

    // load your services (do not forget to include them in your spec runner !) 
    // there should be myService defined in this module 
    beforeEach(module('myservices')); 

    // It seems a good practice to inject nothing in "it" 
    // Everything is injected in beforeEach 
    // here the controller uses theService and set some value in the scope 
    beforeEach(inject(function($rootScope, $controller, ReportProductService) { 
     scope = $rootScope.$new(); 
     ctrl = $controller('myController', { 
     $scope: scope, 
     theService : myService 
     }); 
    })); 

    it('should work', function() { 

    //verify that the controller is there 
    expect(ctrl).toBeDefined(); 

    // do test 
    expect(scope.someExpectedValueSetByController).toBeDefined(); 
}); 

我可以做的jsfiddle如果你想
我也可以做一個更好的答案,我的服務使用$ HTTP

請評論,如果你不不同意或找到更好的方法去做

相關問題