简体   繁体   English

使用Jasmine进行Angular JS单元测试

[英]Angular JS Unit Testing using Jasmine

I have a factory service as below 

FamilyService.js FamilyService.js

(function(){ 'use strict'; (function(){'use strict';

angular.module('app')
.factory('ABC', FamilyService);

FamilyService.$inject = ['DEF'];
function FamilyService(DEF) {


function returnTrue() {
    var a="true";

}       

}

}()); }());

    I want to call returnTrue method in My test case

 **TestFamilyService.js**

describe('testing my_controller.my_function', function () { var mockedFactory, $rootScope, $controller; describe('testing my_controller.my_function',function(){var mockedFactory,$ rootScope,$ controller;

  beforeEach(module('app', function($provide) {
    mockedFactory = {
      save: jasmine.createSpy()
    };

    $provide.value('ABC', mockedFactory);
  }));




  it('should call the save function', function() {

    expect(mockedFactory.returnTrue()).toHaveBeenCalled();
  });
});



     **specrunner.html**


<!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Jasmine Spec Runner v2.4.1</title>

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

      <script src="lib/jasmine-2.4.1/jasmine.js"></script>
      <script src="lib/jasmine-2.4.1/jasmine-html.js"></script>
      <script src="lib/jasmine-2.4.1/boot.js"></script>
      <script src="lib/jasmine-2.4.1/angular.js"></script>
      <script src="lib/jasmine-2.4.1/angular-mocks.js"></script>


      <!-- include source files here... -->
      <script src="src/Player.js"></script>
      <script src="src/webapp/Mock.js"></script>
      <script src="src/webapp/FamilyService.js"></script>




      <!-- include spec files here... -->

      <script src="spec/TestFamilyService.js"></script>

    </head>

    <body>
    </body>
    </html>

When i run specrunner.html in the browser it displays 当我在浏览器中运行specrunner.html时,它将显示

Error: No module: app 错误:无模块:应用程序

TypeError: Unable to get value of the property 'returnTrue': object is null or undefined TypeError:无法获取属性'returnTrue'的值:对象为null或未定义

Please Tel me whats wrong here? 请给我打电话,这里怎么了?

In FamilyService.js 在FamilyService.js中

angular.module('app').factory('ABC', FamilyService);

you are creating a factory for the existing module app , but if the module doesn't exist you need to write 您正在为现有的模块 app创建工厂,但是如果该模块不存在 ,则需要编写

angular.module('app',[]).factory('ABC', FamilyService);

In your case just create a new file , where the angular module gets created 在您的情况下,只需创建一个新文件 ,即可在其中创建angular模块

angular.module('app',[]);

UPDATE 更新

Just include the dependencies of your module in the html 只需在html中包含模块的依赖项

 <script src="lib/angular-sanititze/angular-sanitize.js"></script>

If you want to mock these modules, you can do something like 如果要模拟这些模块,则可以执行以下操作

beforeEach(function() {
       angular.module('second',[]);
}

But in the case that you are using some methods of the third party library, you need to mock these methods as well. 但是,如果您使用的是第三方库的某些方法,则还需要模拟这些方法。

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

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