0

我不能在我的 karma 测试中使用我的变量 $scope.films 并且我不知道我在哪里失败了:

电影服务.js:

(function () {
    'use strict';

    var filmsServices = angular.module('filmsServices', ['ngResource']);

    filmsServices.factory('Films', ['$resource',
      function ($resource) {
          return $resource('Controllers/films/', {}, {
              query: { method: 'GET', params: {}, isArray: true }

          });
      }]);
})();

电影控制器.js:

(function () {
    'use strict';

    angular
        .module('filmsApp')
        .controller('filmsController', filmsController);

    filmsController.$inject = ['$scope' , 'Films']; 
    function filmsController($scope, Films) {
        $scope.films = Films.query();
    }
})();

电影控制器.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using mvc_template.Models;

// For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860

namespace mvc_template.API.Controllers
{
    [Route("api/[controller]")]
    public class FilmsController : Controller
    {
        // GET: api/values
        [HttpGet]
        public IEnumerable<Film> Get()
        {
            return new List<Film>
            {
                new Film {Id=1, Title="Star wars",Director="Lucas"  },
                new Film {Id=1,Title="king Kong", Director="Jackson" },
                new Film {Id=1,Title="Memento", Director="Nolan" }
            };
        }

    }
}

app.js :

(function () {
    'use strict';

    angular.module('filmsApp', [
        'filmsServices'
    ]);
})();

最后是没有通过的测试:

describe('filmsController function', function () {

    describe('filmsController', function () {
        var $scope,$ctrl;

        beforeEach(module('filmsApp'));

        beforeEach(inject(function ($rootScope, $controller) {
            $scope = $rootScope.$new();
            $ctrl = $controller('filmsController', { $scope: $scope });
            console.log($ctrl);
            console.log("$ctrl.$scope : " + $ctrl.$scope);
        }));


        it('should create "films" model with 3 films', function () {
            console.log($scope);
            console.log("$scope.films[1] : "+$scope.films[1]);
            expect($scope.films.length).toBe(3);
        });
    });
});   

业力返回:

C:\Users\kteisseire\Documents\Visual Studio 2015\Projects\mvc-template\src\mvc_t
emplate>karma start karma.conf.js
INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
    INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Windows 8)]: Connected on socket YZ7S3hiUX7TK3wDcHU9q wit
h id 60523108
LOG: filmsController{}
LOG: '$ctrl.$scope : undefined'
LOG: Scope{$$childTail: null, $$childHead: null, $$nextSibling: null, $$watchers
: null, $$listeners: Object{}, $$listenerCount: Object{}, $$watchersCount: 0, $i
    d: 2, $$ChildScope: null, $parent: Scope{$id: 1, $$childTail: Scope{$$childTail:
            ..., $$childHead: ..., $$nextSibling: ..., $$watchers: ..., $$listeners: ..., $
        $listenerCount: ..., $$watchersCount: ..., $id: ..., $$ChildScope: ..., $parent:
            ..., $$prevSibling: ..., films: ...}, $$childHead: Scope{$$childTail: ..., $$ch
            ildHead: ..., $$nextSibling: ..., $$watchers: ..., $$listeners: ..., $$listenerC
            ount: ..., $$watchersCount: ..., $id: ..., $$ChildScope: ..., $parent: ..., $$pr
            evSibling: ..., films: ...}, $$prevSibling: null, $$nextSibling: null, $$watcher
        s: null, $parent: null, $$phase: null, $root: Scope{$id: ..., $$childTail: ...,
            $$childHead: ..., $$prevSibling: ..., $$nextSibling: ..., $$watchers: ..., $pare
            nt: ..., $$phase: ..., $root: ..., $$destroyed: ..., $$listeners: ..., $$listene
            rCount: ..., $$isolateBindings: ..., $$asyncQueue: ..., $$postDigestQueue: ...,
            $$applyAsyncQueue: ..., $$ChildScope: ...}, $$destroyed: false, $$listeners: Obj
        ect{}, $$listenerCount: Object{}, $$isolateBindings: null, $$asyncQueue: [...],
        $$postDigestQueue: [], $$applyAsyncQueue: [], $$ChildScope: function ChildScope(
        ) { ... }}, $$prevSibling: null, films: []}
LOG: '$scope.films[1] : undefined'
PhantomJS 1.9.8 (Windows 8) filmsController function filmsController should crea
te "films" model with 3 films FAILED
        Expected 0 to be 3.
            at C:/Users/kteisseire/Documents/Visual Studio 2015/Projects/mvc-tem
plate/src/mvc_template/js/tests/controller.test.js:19
PhantomJS 1.9.8 (Windows 8): Executed 5 of 5 (1 FAILED) (0.028 secs / 0.018 secs
)
4

0 回答 0