NG,Typescript和Jasmin测试失败

时间:2015-12-29 06:07:24

标签: javascript angularjs typescript jasmine

getQuestions被调用但似乎永远不会完成,所以测试中的长度总是未定义的。但是,当我在没有单元测试的情况下直接访问页面时,这可以正常工作。有什么想法吗?提前谢谢!

    //service
            `/// <reference path="../entity/entity.service.ts" />
                module app.services {
                'use strict';

                //TODO: answer question and favorite
                export interface IQuestionService {
                    favoriteQuestion(favQuestion: entity.IFavoriteQuestionDetails): angular.IHttpPromiseCallbackArg<any>
                    ask(question: entity.IQuestionDetails):angular.IHttpPromiseCallbackArg<any>;
                    getQuestions(): angular.IPromise<app.entity.IQuestions>;
                    getQuestion(qid: string): angular.IHttpPromiseCallbackArg<entity.IQuestion>;
                    answerQuestion(answer: entity.AnswerDetails): angular.IHttpPromiseCallbackArg<any> 
                    sort(sortType: entity.SortType): angular.IHttpPromiseCallbackArg<app.entity.IQuestions>;
                    search(q: string): angular.IHttpPromiseCallbackArg<app.entity.IQuestions>;

                }

                export class QuestionService implements IQuestionService  {
                    static $inject = [
                        '$http'
                    ];
                    constructor(private $http: angular.IHttpService) {


                    }

                    favoriteQuestion(favQuestion: entity.IFavoriteQuestionDetails): angular.IHttpPromiseCallbackArg<any> {

                        return this.$http
                            .post('https://api.stackexchange.com/2.2/' + favQuestion.id + '/favorite', favQuestion)
                            .then((response: angular.IHttpPromiseCallbackArg<any>): any => {
                                return response.data;
                            }, () => {
                                return null;
                            });

                    }

                    getQuestions(): angular.IPromise<app.entity.IQuestions> {
                        return this.$http
                            .get('https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&site=stackoverflow')
                            .then((response: angular.IHttpPromiseCallbackArg<app.entity.IQuestions>): app.entity.IQuestions => {


                                return response.data;
                            }, () => {
                                return null;
                            });

                    }

                    getQuestion(qid: string): angular.IHttpPromiseCallbackArg<app.entity.IQuestion> {
                        return this.$http
                            .get('http://api.stackexchange.com//2.2/questions/' + qid + '/answers?order=desc&sort=activity&site=stackoverflow&filter=withbody')
                            .then((response: angular.IHttpPromiseCallbackArg<entity.IQuestion>): entity.IQuestion => {


                                return response.data;
                            }, () => {
                                return null;
                            });

                    }

                    //https://api.stackexchange.com/docs/create-answer
                    ///2.2/questions/ { id } / answers / add
                    answerQuestion(answer: entity.AnswerDetails): angular.IHttpPromiseCallbackArg<any> {

                        return this.$http
                            .post('https://api.stackexchange.com/2.2/' + answer.id + '/answers/add', answer)
                            .then((response: angular.IHttpPromiseCallbackArg<any>): any => {
                                return response.data;
                            }, () => {
                                return null;
                            });

                    }


                    sortStr: string;

                    sort(sortType: app.entity.SortType): angular.IHttpPromiseCallbackArg<entity.IQuestions> {

                        switch (sortType) {
                        case app.entity.SortType.Creation:
                            this.sortStr = 'creation';
                            break;
                        case app.entity.SortType.Hot:
                            this.sortStr = 'hot';
                            break;
                        case app.entity.SortType.Month:
                            this.sortStr = 'month';
                            break;
                        case app.entity.SortType.Votes:
                            this.sortStr = 'votes';
                            break;
                        case app.entity.SortType.Week:
                            this.sortStr = 'week';
                            break;
                        default:
                            this.sortStr = 'activity';
                            break;
                        }

                        return this.$http
                            .get('http://api.stackexchange.com/2.2/questions?order=asc&sort=' + this.sortStr + '&site=stackoverflow ')
                            .then((response: angular.IHttpPromiseCallbackArg<entity.IQuestions>): entity.IQuestions => {


                                return response.data;
                            }, () => {
                                return null;
                            });

                    }


                    search(q: string): entity.IQuestions {


                        return <any>(this.$http
                            .get('https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + q + 'site=stackoverflow')
                            .then((response: angular.IHttpPromiseCallbackArg<entity.IQuestions>): entity.IQuestions => {


                                return response.data;
                            }, () => {
                                return null;
                            }));

                    }




                    ask(question: entity.QuestionDetails): angular.IHttpPromiseCallbackArg<any> {

                        return this.$http
                            .post('https://api.stackexchange.com/2.2/questions/add', question)
                            .then((response: angular.IHttpPromiseCallbackArg<any>): any => {
                                return response.data;
                            }, () => {
                                return null;
                            });


                    }



                }

                angular.module('app.services').service('app.services.QuestionService', QuestionService);
            }


        //controller

            /// <reference path="../entity/entity.service.ts" />
            module app.questions {
                'use strict';
                export interface IQuestions {
                    getQuestions(): void;
                }
                export class QuestionsController implements IQuestions {
                    static $inject = [
                        '$scope',
                        'app.services.QuestionService'
                    ];

                    allquestions: entity.IQuestions;

                    constructor(
                        private $scope: angular.IScope,
                        private questions: app.services.IQuestionService

                    ) {

                    }
                    getQuestions(): void {
                        this.questions.getQuestions().then(
                            response => {
                                this.$scope['questions'] = response;
                                console.log('questions' + response);
                            });
                    }
                }
                angular
                    .module('app.questions')
                    .controller('app.questions.QuestionsController', QuestionsController);
            }


//unit test
                describe('QuestionController', function () {
                beforeEach(angular.mock.module('app'));

                var $controller;

                beforeEach(inject(function (_$controller_) {
                    // The injector unwraps the underscores (_) from around the parameter names when matching
                    $controller = _$controller_;
                }));

                describe('do we have questions', function () {
                    var $scope, controller, qs;

                    beforeEach(function (done) {
                        $scope = {};
                        qs = {};
                        controller = $controller('app.questions.QuestionsController', {   $scope: $scope });

                   controller.getQuestions();
                        setTimeout(function () {

                            done();
                        }, 3000);
                    });
                    it("the list should have questions", function (done) {

                        expect($scope.questions.items.length).toBeGreaterThan(0);
                        done();
                    });
                    });
                });`

0 个答案:

没有答案