2015-02-03 4 views
0

options.url 매개 변수 initilaize의 매개 변수에 대한 테스트를 작성하려면 어떻게해야합니까? Backbone.Collection?모어를 사용하여 백본 컬렉션 초기화 기능 테스트

다음 코드가 있는데 테스트를 작성하는 데 문제가 있습니다. Mocha, ChaiSinonjs을 사용하고 있습니다. 내가 코멘트 아웃 때 this.url = options.url을 내 백본 컬렉션에서 테스트를 통과,하지만 난 그것을 주석을 해제 할 때 나는 다음과 같은 주장의 오류 얻을 :

TypeError: 'undefined' is not an object (evaluating 'options.url')

내 백본 코드 :

Foo.Collection._entity = Backbone.Collection.extend({ 
    url: '',  //API's endpoint for this collection 
    fetched: false, //flag to avoid fetching twice if consecutive fetches occur 

    /** 
    * Makes sure the URL is set 
    * @param {Object[]} models Set of initial models 
    * @param {Object} options Regular initialization options object 
    */ 
    initialize: function (models, options) { 
     models = models || []; 
     this.url = options.url; 
    } 
}); 

내 테스트를 이 컬렉션의 경우

describe("Foo.Collection._entity", function() { 

    beforeEach(function() { 
     // Sinon fake server for the backend 
     this.server = sinon.fakeServer.create(); 

     // Server automatically responds to XHR requests 

     this.server.autoRespond = true; 

     this.entity = new Foo.Collection._entity(); 

    }); 

    afterEach(function() { 
     // stop fake server 
     this.server.restore(); 
    }); 

    describe("Default values", function() { 

     // test default values 
     it("has default values", function() { 
      expect(this.entity).to.be.ok; 
      expect(this.entity).to.have.length(0); 
     }); 

     it("default url to be ' '",function() { 
      var entity = this.entity; 
      expect(entity.url).to.be.equal(""); 
     }); 

     it("fetched flag to be set to false", function(){ 
      var entity = this.entity; 
      expect(entity.fetched).to.be.equal(false); 
     }); 
    }); 
}); 

this.url = options.url 스 니펫을 전달하는 테스트는 어떻게 작성합니까?

답변

0

예를 들어, beforeEach 함수에서 올바른 매개 변수를 사용하여 Collection을 인스턴스화 한 다음 Collection의 URL과 길이를 테스트하는 것만 큼 간단합니다.

this.entity = new Foo.Collection._entity({options: 
      { 
       url: "/Foo" 
      } 
     }); 

    describe("Default values", function() { 

     // test default values 
     it("has default values", function() { 
      expect(this.entity).to.be.ok; 
      expect(this.entity).to.have.length(1); 
      expect(entity.url).to.be.equal("Foo"); 
     });