[Unit Testing] Angular Unit Testing, ui-router, httpbackend and spy
2015-12-11 03:29
585 查看
// backend test beforeEach(inject(function (_$compile_, _$httpBackend_, _$rootScope_, _$state_, _AnnouncementsService_, _CONFIG_) { compile = _$compile_; $httpBackend = _$httpBackend_; $scope = _$rootScope_.$new(); AnnouncementsService = _AnnouncementsService_; CONFIG = _CONFIG_; // Need to mock $state, so the ui-router resolve wont conflict with tests. state = _$state_; spyOn( state, 'go'); spyOn( state, 'transitionTo'); directiveElem = getCompiledElement(); directiveCtrl = directiveElem.controller('comAnnouncements'); })); afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); it('should respond 200 to a http get request and get 1 announcement', function(){ AnnouncementsService.getAnnouncementData(2); let expectedResponse = [ { "id": 0, "title": "Maintenance work may affect Internet services 20/09/2015", "date": "11 Nov 2015 | 12:30 PM" } ]; $httpBackend.expectGET(CONFIG.BACKEND_API_URL + '/announcements/2').respond(200, { 'announcements': expectedResponse }); $httpBackend.flush(); expect(AnnouncementsService.announcementData).toEqual(expectedResponse); }); // ui-router test it('Should move to "home state"', () => { $scope.$apply(() => { angular.element(directiveElem.find('a')[0]).click(); $timeout.flush(); }); expect($state.current.name).toEqual('selfcare.home'); }); it('$state href should equal "/home"', () => { $scope.$apply(() => { $state.go('selfcare.home'); }); expect($state.current.name).toEqual('selfcare.home'); expect($state.href('selfcare.home')).toEqual('/home'); }); // Click to expect function to be called it('should call goToSearch()', function(){ spyOn(directiveCtrl, 'goToSearch'); angular.element(directiveElem.find('.header-icon-menu i')[0]).click(); expect(directiveCtrl.goToSearch).toHaveBeenCalled(); });
相关文章推荐
- SDP, RTP, RTCP, RTSP, RTMP 名词解释
- iOS网络编程(一)
- A20_CORE网络问题之硬件问题!!!
- Chapter 7-01
- //监测网络状态(AFNetworking) 服务端 客户端
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- 用httpPost对JSON发送和接收的例子
- 使用ConcurrentQueue并发集合来设计网络服务器端
- TCP 协议状态解析
- 自己动手写网络爬虫(第一天)
- Android Fiddler 手机抓包 Http 数据
- Android中使用代码控制Wifi及数据连接网络开关
- B树、B-树、B+树、B*树http://www.cnblogs.com/oldhorse/archive/2009/11/16/1604009.html
- 异步block网络请求
- 10G(82599EB) 网卡测试优化(jumbo frame, tcp win scaling)
- 网络基础
- http权威指南-第二章-URL与资源
- ImageLoader(UIL)自定义HTTP Header信息
- 网络地址转换(NAT)
- HTTP server git