设置Jasmine AngularJS服务测试时的未知提供程序

时间:2014-05-19 14:44:53

标签: asp.net angularjs jasmine

我有一个angularjs服务,根据折扣,数量等计算产品价格。我正在尝试编写一个茉莉花测试来调用此服务,传入测试数据。我收到错误,该应用程序缺少它的依赖项。我不想加载Ui路由器,不应该嘲笑那个吗?

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
                });
            }
        }
    }
])

2 个答案:

答案 0 :(得分: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/ $ injector

答案 1 :(得分: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

如果您不同意或找到更好的方法,请发表评论

相关问题