Javascript 无法将伪数组绑定到测试中的作用域变量

Javascript 无法将伪数组绑定到测试中的作用域变量,javascript,angularjs,unit-testing,angularjs-directive,karma-runner,Javascript,Angularjs,Unit Testing,Angularjs Directive,Karma Runner,我无法在指令测试中将伪数组绑定到范围变量 我的测试: describe('Directive: report - section', function () { // load the directive's module beforeEach(module('ReportApp')); beforeEach(module('Templates')); // The external template file referenced by templateUrl var el

我无法在指令测试中将伪数组绑定到范围变量

我的测试:

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);

  }));


});
我的测试结果:

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)

原因是您的
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);

  }));


});
description('Directive:report-section',function(){
//加载指令的模块
在每个模块之前(模块('ReportApp');
beforeach(模块('Templates');//templateUrl引用的外部模板文件
var要素、范围、报告;
beforeach(inject(函数($rootScope,\u report\u){
scope=$rootScope.$new();
report=\u report;//注入report对象并存储在变量中
}));
它('should have 1 section available',inject(函数($compile){
var testSections=[
{
id:'Example01',
可见:对,
img:'image1.jpg'
},
{
id:'Example02',
可见:假,
img:'image2.jpg'
}
];
spyOn(report,“getDatabase”).and.returnValue({sections:testSections});//监视getDatabase函数
//我们只需要一个存根,所以我们也可以这样写:
//report.getDatabase=函数(){
//返回{sections:testSections};
//}
元素=角度。元素(“”);
元素=$compile(元素)(范围);
范围。$digest();
expect(element.find('li').length).toEqual(1);
}));
});

Hi@Khanh to我到目前为止还没能成功。它返回一个错误:TypeError:无法读取未定义的属性'returnValue'。但这没有意义,因为如果我控制台.log('report:',report');把所有的东西都给我看看。@Ventura:你的茉莉花版本是什么?@Ventura:在这种情况下,我们实际上不需要茉莉花。你可以试试我写的代码commented@Ventura:如果您使用jasmine 1.3,请尝试
spyOn(report,“getDatabase”).andReturn({sections:testSections})
scope.sections = 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);

  }));


});