简体   繁体   English

使用Jasmine单元测试模拟AngularJS Web服务

[英]Mocking AngularJS Webservice using Jasmine Unit Test

I want to mock my Angular JS method using Jamine. 我想使用Jamine模拟我的Angular JS方法。 My code is:- 我的代码是:-

<script type="text/javascript">

var app = angular.module('mymodulee', []);
   app.controller('mycontroller', function ($scope, $http, $log) {
    $scope.ButtonClick = function (Id) {

        var response = $http({
            method: "get",
            url: http://localhost:8080/Access/Tasks.DefectManagement/Services/Services.asmx/GetEligibilityDetails",
            params: {
                CovId: Id
            }

        });
        return response;
    }
});

And my Jasmine Test Case is:- 我的茉莉花测试用例是:-

    it('EligibilityDetails', function () {

    var myserv, httpBackend;

    inject(function ($httpBackend, _myserv_) {
        myserv = _myserv_;
        httpBackend = $httpBackend;
    });



afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});
    var $scope = {};
    var controller = $controller('PatientDefectManagementCtrl', { $scope: $scope });

    var returnData = {};
    httpBackend.expectGET("http://localhost:8080/Access/Tasks.DefectManagement/Services/Services.asmx/GetEligibilityDetails").respond(returnData);

    var returnedPromise = myserv.get(3904142);

    var result;
    returnedPromise.then(function (response) {
        result = response.data;
    });

    httpBackend.flush();

    expect(result).toEqual(returnData);



});

But its is giving an error. 但是它给出了一个错误。 Can anyone please tell what changes I should make in my code so that i can run the test case using Jasmine Unit test. 任何人都可以告诉我应该对代码进行哪些更改,以便可以使用Jasmine Unit test运行测试用例。

Please help. 请帮忙。

Thanks 谢谢

 describe('test',function(){
 afterEach(function () {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();});
 var myserv, httpBackend;
beforeEach(inject(function(_$httpBackend_,_myserv__){
myserv = _myserv_;
httpBackend = $httpBackend;});
it('EligibilityDetails', function () {
var $scope = {};
var controller = $controller('PatientDefectManagementCtrl', { $scope: $scope });
var returnData = {};
httpBackend.expectGET("http://localhost:8080/Access/Tasks.DefectManagement/Services/Services.asmx/GetEligibilityDetails").respond(returnData);

var returnedPromise = myserv.get(3904142);

var result;
returnedPromise.then(function (response) {
    result = response.data;
});

httpBackend.flush();

expect(result).toEqual(returnData);});});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM