错误:意外请求:GET / api / movies

时间:2015-02-14 16:56:34

标签: angularjs jasmine

当我运行以下代码时,我收到以下错误。 结果消息:

错误:意外请求:GET / api / movies

代码在gist链接中更新。只是想知道我做错了什么还是需要配置其他东西。

https://gist.github.com/rahulsahay19/041ca130d187e2a6009e

谢谢, 拉胡

1 个答案:

答案 0 :(得分:0)

解决。我在一个对象中包装响应,然后返回相同的内容。

/// <reference path="../scripts/jasmine.js" />
/// <reference path="../../moviereview.web/scripts/angular.min.js" />
/// <reference path="../../moviereview.web/scripts/ui-bootstrap-tpls.min.js" />
/// <reference path="../../moviereview.web/scripts/angular-route.min.js" />
/// <reference path="../../moviereview.web/scripts/angular-mocks.js" />
/// <reference path="../../moviereview.web/js/homeindex.js" />
/// <reference path="../../moviereview.web/js/movie-review-edit.js" />

describe("home-Index Tests-->", function () {

   
    beforeEach(function () {
        module("homeIndex");
    });


    //to test individual bits and bytes inside the home-Index
    describe("dataService-->", function () {

        it("can load movies", inject(function (dataService) {
            //for the 1st Run
            expect(dataService.movies.length).toEqual(0);
        }));
    });

    //$httpbackend service
    var $httpBackend;
    var url = '/api/movies';
    
    var fakedMoviesResponse=[{
            Id: 1,
            MovieName: "Godzilla",
            DirectorName: "Gareth Edwards",
            ReleaseYear: "2014",
            NoOfReviews: 6
        },
            {
                Id: 3,
                MovieName: "Titanic",
                DirectorName: "James Cameron",
                ReleaseYear: "1997",
                NoOfReviews: 3
            },
            {
                Id: 4,
                MovieName: "Die Another Day",
                DirectorName: "Lee Tamahori",
                ReleaseYear: "2002",
                NoOfReviews: 0
            },
            {
                Id: 7,
                MovieName: "Taken 3",
                DirectorName: "Olivier Megaton",
                ReleaseYear: "2014",
                NoOfReviews: 0
            },
            {
                Id: 9,
                MovieName: "Top Gun",
                DirectorName: "Tony Scott",
                ReleaseYear: "1986",
                NoOfReviews: 0
            }
        ];
    beforeEach(inject(function ($injector) {

        $httpBackend = $injector.get("$httpBackend");

        $httpBackend.whenGET(url)
            .respond(fakedMoviesResponse);
        
    }));

    afterEach(function () {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });
    //test the backend call
    
    describe("Testing Movies GET Call-->", function () {

        it("Loaded Movies", inject(function (dataService) {
            $httpBackend.expectGET(url);
            dataService.getMovies();
            $httpBackend.flush();
            expect(dataService.movies.length).toEqual(5);
        }));
    });
})