用茉莉花编写单元测试用例角JS [英] Writing Unit Test Case in Angular JS using Jasmine

查看:178
本文介绍了用茉莉花编写单元测试用例角JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立使用角JS的应用程序。由于我是新来这我不知道很多关于它编写测试用例。

I am building an application using Angular JS. As I am new to it I don't know much about writing test cases in it.

假设我有一个服务:

angular.module('MyApp').

factory('MainPage', function($resource,BASE_URL){

return $resource("my api call", {}, {query: {method:'GET'}, isArray:true});

}).

我的控制器:

var app = angular.module('MyApp')

app.controller('MainCtrl',function($scope,MainPage,$rootScope){
$scope.mainpage = MainPage.query();
    });

我怎么写测试用例使用茉莉花此控制器的角度JS。

How I write test case for this controller in Angular JS using Jasmine.

推荐答案

您会写几行内容:

describe('MyApp controllers', function() {

  describe('MainCtrl', function(){

    it('should populate the query', function() {
      var scope = {},
          ctrl = new MainCtrl(scope);

      expect(scope.mainpage).toEqual(someMainPageMock);
    });
  });
});

这是有据可查的,请参见 AngularJS教程的快速参考,它也建议阅读茉莉花文档(!)。

This is well documented, see the AngularJS tutorial for a quick reference, it's also suggested to read the Jasmine docs (!).

您还希望想窥视查询()方法的看到这里如何。

You'd also want to spy on the query() method, see here on how.

这篇关于用茉莉花编写单元测试用例角JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆