我正在尝试使用ngMockE2E模拟后端以进行开发,但出现此错误“意外请求:GET views / home.html”。我也使用路由服务的ui-router。谢谢
的index.html
<!doctype html>
<html lang="en" ng-app="userApp">
<head>
<meta charset="utf-8">
<title> 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 </p>
</div>
</div>
</footer>
</div>
</div>
</body>
</html>
app.js
(function(ng){
var userApp = ng.module("userApp", ["ui.router", "ngMockE2E", "ngResource", "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" }];
$httpBackend.expect("GET", "/login");
$httpBackend.when("GET", "/login").respond(users[0]);
}]);
})(angular);
答案 0 :(得分:1)
您需要将passThrough添加到您的views目录,以便$httpBackend
服务不会捕获该请求:
userApp.run([ "$httpBackend", function($httpBackend){
var users = [{ "id" : "1", "username" : "user1@test.com", "password" : "user1" }];
$httpBackend.expect("GET", "/login");
$httpBackend.when("GET", "/login").respond(users[0]);
$httpBackend.whenGET(/^views\//).passThrough(); // ignore requests to the views directory
}]);