栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将$ stateParams和$ state注入到茉莉角js测试中变得不确定

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将$ stateParams和$ state注入到茉莉角js测试中变得不确定

我认为您可能会误解路由器如何与控制器一起使用。在对控制器进行单元测试时,您没有在执行路由或进入ui-
router状态。这些状态和路由是应用程序正常运行时要执行的触发控制器。但是在单元测试中,您将使用$
controller显式执行控制器。因此,您将完全跳过路由部分。这意味着您需要模拟ui路由器通常为您创建的对象$ stateparams。

describe('Detail Ctrl', function() {  var scope, ctrl, httpBackend, stateparams, listingId;  beforeEach(angular.mock.module("backpageApp"));  //don't need to inject state or stateparams here  beforeEach(angular.mock.inject(function($controller, $rootScope, _$httpBackend_) {    httpBackend = _$httpBackend_;    stateparams = { listingId: 1 }; //mock your stateparams object with your id    //you should be expecting the get request url from the controller, not the route    httpBackend.expectGET('data/' + stateparams.listingId + '.json').respond([{id: 1 }, {id: 2}, {id:3}, {id:4}, {id:5}, {id:6}, {id:7}, {id:8}, {id:9}, {id:10}]);    scope = $rootScope.$new();     //pass your mock stateparams object to the controller     ctrl = $controller("DetailCtrl", {$scope:scope, $stateParams:stateparams});   }));   it('the images for each listing should exist', function() {    httpBackend.flush();     //I don't see images set in your controller, but you     //could check scope.extrainfo here    expect(scope.images).toBe(true)  });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号