当我尝试使用ngmockE2E模拟后端时,我遇到了错误。
" angularjs [ng:areq] Argument' fn'不是一个函数,得到了字符串"
当我删除ngMock服务时,它可以正常工作。
的index.html
<!doctype html>
<html lang="en" ng-app="userApp">
<head>
<meta charset="utf-8">
<title>IT Beams Angular App</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/all.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript" src="js/angular-cookies.js"></script>
<script type="text/javascript" src="js/ui-router.js"></script>
<script type="text/javascript" src="js/angular-resource.js"></script>
<script type="text/javascript" src="js/angular-mocks.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/controllers.js"></script>
<script type="text/javascript" src="js/services.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<header id="header">
<div class="logo col-sm-12 col-lg-4">
<h1>logo</h1>
<ul class="nav nav-pills pull-right">
<li><a ui-sref="home" class="btn btn-success" ui-sref-active="btn-warning">Home</a></li>
<li><a ui-sref="login" class="btn btn-success" ui-sref-active="btn-warning">Login</a></li>
<li><a ui-sref="register" class="btn btn-success" ui-sref-active="btn-warning">Register</a></li>
</ul>
</div>
<div class="text-area col-sm-12 col-lg-8">
<h2>Test</h2>
</div>
</header>
<section id="main">
<div class="container">
<div class="row">
<div ui-view></div>
</div>
</div>
</section>
<footer id="footer">
<div class="container">
<div class="row">
<p class="text-center">Copyright IT Beams 2015</p>
</div>
</div>
</footer>
</div>
</div>
</body>
</html>
app.js
(function(ng){
var userApp = ng.module("userApp", ["ui.router", "ngResource", "ngMockE2E", "ngCookies", "mainCtrl"]);
userApp.config(["$stateProvider", "$urlRouterProvider", "$provide", function($stateProvider, $urlRouterProvider, $provide){
$urlRouterProvider.otherwise( "/" );
$provide.decorator("$httpBackend", ng.mock.e2e.$httpBackendDecorator);
$stateProvider
.state("login", {
url : "/login",
templateUrl : "views/login.html",
controller : "loginCtrl"
})
.state("register", {
url : "/register",
templateUrl : "views/register.html",
controller : "registerCtrl"
})
.state("home", {
url : "/",
templateUrl : "views/home.html",
controller : "homeCtrl"
})
}]);
userApp.run("$httpBackend", function($httpBackend){
var users = [
{ "id" : "1", "username" : "user1@test.com", "password" : "user1" },
{ "id" : "2", "username" : "user2@test.com", "password" : "user2" },
{ "id" : "3", "username" : "user3@test.com", "password" : "user3" },
{ "id" : "4", "username" : "user4@test.com", "password" : "user4" }
];
$httpBackend.whenGET("/login").respond(users);
console.log(testing);
});
})(angular);
帮助表示赞赏。感谢
答案 0 :(得分:1)
run
块设置不正确。
您正在传递一个字符串作为第一个参数而不是函数:
userApp.run("$httpBackend", function($httpBackend) {
...
});
因此错误。
应该是:
userApp.run(function($httpBackend) {
...
});
或者如果使用内联数组表示法,就像使用config
块一样:
userApp.run(["$httpBackend", function($httpBackend) {
...
}]);