我有以下代碼...其中一個測試失敗。請幫助我理解爲什麼,併爲我提供一種糾正方法。爲什麼我的茉莉花測試失敗,我的Angular應用程序
<html>
<head>
<!-- Jasmine References -->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/jasmine-html.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.3/boot.min.js"></script>
<!-- Angular and Angular Mock references -->
<script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular.min.js"></script>
<script type="text/javascript" src="https://code.angularjs.org/1.4.0-rc.2/angular-mocks.js"></script>
<!-- The code we need to test -->
<script type="text/javascript" src="/js/index.js"></script>
</head>
<body></body>
<script type="text/javascript">
var app = angular.module('peoplesearchApp',[]);
app.controller('peopleSearchCtrl',function($scope,$http){
$scope.textChanged = function() {
// obtain the list of user whose name have the given prefix
$http.get('/findusers?q='+$scope.searchString.trim()).success(function(data, status) {
$scope.users = data;
}).error(function(data, status) {
console.log("Error: could not retrieve users");
});
};
// make an initial call to retrieve all users
$scope.searchString = "";
$scope.textChanged();
});
describe('search people app tests', function() {
var $controller, $httpBackend, $scope;
// setup before each test run
beforeEach(module('peoplesearchApp'));
beforeEach(inject(function (_$controller_, _$httpBackend_) {
$controller = _$controller_;
$scope = {};
$httpBackend = _$httpBackend_;
}));
describe('only the users that have their names starting from the search query should be visible', function() {
it('should show all users for a blank query', function() {
$httpBackend.expectGET('/findusers?q=').respond(['Sean','Yaw','Lucy','Eric','Rory','Heyley']);
$controller('peopleSearchCtrl', {$scope: $scope});
$httpBackend.flush();
expect($scope.users).toEqual(['Sean','Yaw','Lucy','Eric','Rory','Heyley']);
});
it('should show only Rory for query=r', function() {
$httpBackend.expectGET('/findusers?q=r').respond(['Rory']);
$controller('peopleSearchCtrl', {$scope: $scope});
$httpBackend.flush();
expect($scope.users).toEqual(['Rory']);
});
});
})
</script>
</html>
編輯:問題是,在第二次測試中,我想發出一個假的http請求之前設置$scope.searchString = "r"
莫名其妙。我不知道該怎麼做。
的錯誤是:
Error: Unexpected request: GET /findusers?q=
Expected GET /findusers?q=r
CodePen:http://codepen.io/amarshanand/pen/MeJXdq
沒有工作(它是否適用於您?只需在文本編輯器中複製/粘貼代碼並運行它,如果您有幾分鐘的話)。也許是因爲我設置了$ scope = {};在beforeEach(注入) – Amarsh
請創建一個jsfiddle或codepen示例 –
完成。 http://codepen.io/amarshanand/pen/MeJXdq – Amarsh