2014-05-15 37 views
1

我是jasmine.js框架的新手。Jasmine.js V2.0.0 - 讀取燈具數據 - TypeError:undefined不是函數在對象

什麼是這個代碼的問題:

beforeEach(function(){ 
    var fixtureUrl  = "/../path/to/templates/home.html"; 
    var html = jasmine.getFixtures().read(fixtureUrl); 
    console.log(html); 
}); 

我得到一個錯誤:

類型錯誤:未定義是不是對象的函數。

TypeError: undefined is not a function at Object<anonymous> (http://{MY_LOCAL_DOMAIN}/test/jasmine2/spec/home.view.spec.js:15:23) 
    at attemptSync (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1510:12) 
    at QueueRunner.run (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1498:9) 
    at QueueRunner.execute (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1485:10) 
    at Spec.Env.queueRunnerFactory (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:518:35) 
    at Spec.execute (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:306:10) 
    at Object.<anonymous> (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1708:37) 
    at attemptAsync (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1520:12) 
    at QueueRunner.run (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1496:16) 
    at QueueRunner.execute (http://{MY_LOCAL_DOMAIN}/test/jasmine2/libs/jasmine-2.0.0/jasmine.js:1485:10) 

我使用: Jasmine.js,茉莉花,html.js和茉莉版本boot.js 2.0.0茉莉花,jQuery的版本2.1.0

在我應用程序中,我使用spec.config.js加載所有必要的文件包括:按以下順序:

paths: { 
    ..., 
    ..., 
    ..., 
    'jasmine': '../test/jasmine2/libs/jasmine-2.0.0/jasmine',  
    'jasmine-html':'../test/jasmine2/libs/jasmine-2.0.0/jasmine-html',  
    'boot': '../test/jasmine2/libs/jasmine-2.0.0/boot',  
    'jasmine-jquery':'../test/jasmine2/libs/jasmine-2.0.0/jasmine-jquery',  
    'sinon': '../test/jasmine2/libs/sinon-1.9.1/sinon', 
} 

而且我定義爲我的規格文件爲:

describe('Home', function() { 

    define(['views/home.view','jasmine','jasmine-jquery'], function(HomeView,Jasmine,JasmineJQuery) { 
    .... 
     var home_view  = new HomeView(); 
     var jasmine  = Jasmine; 
     var jasmine_jquery = JasmineJQuery; 
    .... 
    }); 
    beforeEach(function(){ 
     var fixtureUrl  = "/../path/to/templates/home.html"; 
     var html = jasmine.getFixtures().read(fixtureUrl); 
     console.log(html); 
    }); 
    it('should check home is rendred', function() { 
      {SOME TESTING HERE...} 
    }); 

}); 

任何一個可以幫助我讀燈具數據,並檢查toHaveId( 「{} SOME_DIV_ID」)?

回答

3

您有權責令.js文件順序如下:

<script type="text/javascript" src="../jasmine2/libs/jasmine-2.0.0/jasmine.js"></script> 
<script type="text/javascript" src="../jasmine2/libs/jasmine-2.0.0/jasmine-html.js"></script> 
<script type="text/javascript" src="../jasmine2/libs/jasmine-2.0.0/boot.js"></script> 
<script type="text/javascript" src="../../js/libs/jquery-1.10.1.min.js"></script> 
<script type="text/javascript" src="../jasmine2/libs/jasmine-2.0.0/jasmine-jquery.js"></script> 

在你的spec文件,你可以定義全局視圖對象爲:

describe('View', function() { 
    'use strict'; 
    {VARIABLE_DECLARATIONS} 
    var fixtureUrl = "/path/to/fixture/fixture.html"; 

    define(['views/home.view','jasmine','jasmine-jquery'], 
     function(HomeView,Jasmine,JasmineJQuery) { 
      {CREATE OBJECTS IF NECESSARY...} 

      describe('Initialize', function() { 
       beforeEach(function(){ 
         var html = jasmine.getFixtures().read(fixtureUrl); 
         jasmine.getFixtures().set(html);    
       }); 
       it('should check initializeView is called ', function() { 
        {YOUR_CODE_HERE...for test from the fixtures}; 
        //expect($('{CONTAINER_ELEMENT_ID}').get(0)).toHaveId("{DIV_ID}"); 
       }); 
      });       
    }); 
}); 

我希望這有助於。

相關問題