gpt4 book ai didi

javascript - 使用 requirejs 测试 uiRouter

转载 作者:行者123 更新时间:2023-12-03 12:03:52 24 4
gpt4 key购买 nike

我已经在这个问题上停留了一段时间,并且没有成功解决这里有关使用 requirejs 测试 uiRouter 的任何其他问题。我有一个相当基本的 Controller 设置,一旦单击按钮,它就会使用 $state.go 在状态之间进行转换。

runsController.js

define([],
function() {
"use strict"
var runsController = function($scope, $window, $http, $state) {

function getRuns() {
$http.get($window.apiLinks.runs).success(function(data) {
$scope.runs = data.objects;
}).error(function(data) {
console.error();
console.error(data);
});
}
getRuns();

$scope.refresh = getRuns;



$scope.createRun = function() {
//$state.go('createRun');
}

return ["$scope", "$window", "$http", "$state", runsController];
});

Controller 已添加到依赖于 uiRouter 的应用程序。

app.js

define(["angular", "js/controllers/runsController", "js/router", "uiRouter"],
function(angular, runsController, router) {
'use strict'

var appName = "myApp";
var app = angular.module(appName, ["ui.router"]);

app.config(router);

app.controller("runsController", runsController);


function getName() {
return appName;
}

return {
app : app,
getName : getName
};

});

路由器.js

define(["./controllers/runsController"],
function(runsController){

var routes = function($stateProvider, $urlRouterProvider) {

// For any unmatched url, redirect to /runs
$urlRouterProvider.otherwise("/runs");

// Set up the states
$stateProvider
.state('runs', {
url: "/runs",
templateUrl: "partials/runs.html",
controller: "runsController"

})
.state('createRun', {
url: "/createRun",
templateUrl: "partials/runCreator.html"
});
};


return ["$stateProvider", "$urlRouterProvider", routes];

});

这是我为此 Controller 设置的测试:

define(["angular", "angularMocks", "js/app", "js/controllers/runsController"],
function(angular, mocks, app, runsController) {
'use strict'
describe('runsController Unit Tests', function() {
var mockApp, scope, httpBackend, objects, state;


objects = [
{rid : 1, filename : "myFile.txt", exitCode : 0},
{rid : 2, filename : "test.zip", exitCode : 0},
{rid : 3, filename : "test2.tar", exitCode : 0}
];

beforeEach(function() {

mockApp = angular.module(app.getName());
});

beforeEach(inject(function ($rootScope, $controller, $httpBackend, $http, $state) {
scope = $rootScope.$new();
window.apiLinks = {"runs" : "/mock/api/runs"};
httpBackend = $httpBackend;
state = $state;
httpBackend.when("GET", "/mock/api/runs").respond({
"objects" : objects
});
$controller(runsController[4], {
$scope : scope,
$window : window,
$http : $http,
$state : $state
});
httpBackend.flush();
}));

it("Get state working in test environment", function() {

});

it("Test that controller automatically gets the runs", function() {
// Because of the way our controller files are setup, we have to specify the
// last element in the array instead of just
expect(scope.runs.length).toBe(3);
});


});

当前给我一条错误消息:错误:[$injector:unpr] 未知提供者:$stateProvider <- $state

根据我所读到的内容,我相信这意味着我需要注入(inject) Controller 运行状态所需的依赖项,但是如果我在每次运行测试之前使用该应用程序,那么依赖项应该在以下情况下存在: .config() 做得对吗?谁能帮我弄清楚我没有看到什么?

提前致谢!

最佳答案

我也遇到了同样的问题,这就是我想到的。

这非常重要:

karma.conf.js

 {
pattern: 'ROOT/TO/YOUR/angular-ui-router/release/angular-ui-router.js',
included: false
},

test-main.js:这是你的 karma - require - 配置文件

 paths: {
// External libraries
'angular': '/base/PATH/TO/YOUR/angular/angular',
'angularMocks': '/base/PATH/TO/YOUR/angular-mocks/angular-mocks',
'uiRouter' : '/base/PATH/TO/YOUR/angular-ui-router/release/angular-ui-router',
.....
  • /base/ + 你的路径

做摇摆 Action

 shim: {
'angular': { 'exports': 'angular' },
'angularMocks': { deps: ['angular'], 'exports': 'angular.mock' },
'uiRouter' : {deps: ['angular'], 'exports' : 'uiRouter'},

现在我们正在进行由内而外的测试。

定义模块

define([
'angular',
'angularMocks',
'uiRouter',
.....

现在,当您设置 beforeEach

BeforeEach(inject(function ($injector){
$state = $injector.get('$state');
....

希望这对您有所帮助。

关于javascript - 使用 requirejs 测试 uiRouter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25274030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com