【问题标题】:Unable to bind my fake array to a scope variable in my test无法将我的假数组绑定到我的测试中的范围变量
【发布时间】:2014-08-09 12:04:32
【问题描述】:

我无法将我的假数组绑定到指令测试中的范围变量。

我的测试:

describe('Directive: report - section', function () {

  // load the directive's module
  beforeEach(module('ReportApp'));
  beforeEach(module('Templates')); // The external template file referenced by templateUrl

  var element, scope;

  beforeEach(inject(function ($rootScope) {
    scope = $rootScope.$new();
  }));


  it('should have 1 section available', inject(function ($compile) {

    var testSections = [
        {
            id: 'Example01',
            visible: true,
            img: 'image1.jpg'
        },
        {
            id: 'Example02',
            visible: false,
            img: 'image2.jpg'
        }
    ];

    scope.sections = testSections;
    element = angular.element('<section></section>');
    element = $compile(element)(scope);
    scope.$digest();
    expect(element.find('li').length).toEqual(1);

  }));


});

我的指令:

angular.module('ReportApp')
  .directive('section', function (report, reportStatus) {
    return {
      templateUrl: 'src/report/views/parts/section.html',
      restrict: 'E',
      controller: function( $scope, $element, $attrs){

        var sections = report.getDatabase().sections;

        $scope.sections = sections;
        reportStatus.setActiveSection(sections[0]);

      },

      link: function postLink(scope, element, attrs) {

      }

    };
  });

我的测试结果:

Chrome 36.0.1985 (Mac OS X 10.9.2) Directive: report - section should have 1 section available FAILED
  Expected 4 to equal 1.
  Error: Expected 4 to equal 1.
      at null.<anonymous> (/Users/user/MyAPPs/temp/report/app/src/report/directives/tests/spec/section.js:77:39)
      at Object.invoke (/Users/user/MyAPPs/temp/report/app/vendor/bower_components/angular/angular.js:3678:17)
      at workFn (/Users/user/MyAPPs/temp/report/app/vendor/bower_components/angular-mocks/angular-mocks.js:2102:20)

我的问题是假部分(testSections)没有被应用。所以,这个结果“预期 4 等于 1”是由于使用了原始部分而不是我的假部分。

为什么这个范围不起作用?

scope.sections = testSections;

【问题讨论】:

    标签: javascript angularjs unit-testing angularjs-directive karma-runner


    【解决方案1】:

    原因是您的指令代码中的scope.sections = testSections;$scope.sections = sections; 替换。

    在这种情况下,您必须监视您的 report.getDatabase() 才能返回您的 testSections

    describe('Directive: report - section', function () {
    
      // load the directive's module
      beforeEach(module('ReportApp'));
      beforeEach(module('Templates')); // The external template file referenced by templateUrl
    
      var element, scope, report;
    
      beforeEach(inject(function ($rootScope,_report_) {
        scope = $rootScope.$new();
        report = _report_; //inject the report object and store in a variable
      }));
    
    
      it('should have 1 section available', inject(function ($compile) {
    
        var testSections = [
            {
                id: 'Example01',
                visible: true,
                img: 'image1.jpg'
            },
            {
                id: 'Example02',
                visible: false,
                img: 'image2.jpg'
            }
        ];
    
        spyOn(report,"getDatabase").and.returnValue({ sections : testSections });//spy the getDatabase function
        //we just need a stub, so we could also write this:
    
        //report.getDatabase = function (){
        //       return { sections : testSections };
        //}
    
        element = angular.element('<section></section>');
        element = $compile(element)(scope);
        scope.$digest();
        expect(element.find('li').length).toEqual(1);
    
      }));
    
    
    });
    

    【讨论】:

    • 嗨@Khanh-to 到目前为止我无法让它工作。它返回一个错误:TypeError: Cannot read property 'returnValue' of undefined.
    • 但这没有任何意义,因为如果我 console.log('report: ', report);正确显示所有对象。
    • @Ventura:你的 jasmine 版本是什么?
    • @Ventura:在这种情况下,我们实际上不需要茉莉花。你可以试试我评论的代码
    • @Ventura:如果你使用 jasmine 1.3,试试spyOn(report,"getDatabase").andReturn({ sections : testSections });
    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2013-04-29
    • 2014-01-24
    • 2021-04-16
    • 2021-10-18
    相关资源
    最近更新 更多