Jasmine.js V2.0.0 - 读取fixture数据 - TypeError:undefined不是Object的函数

时间:2014-05-15 11:06:09

标签: jquery backbone.js jasmine jasmine-jquery

我是jasmine.js框架的新手。

此代码中存在的问题是什么:

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

我收到错误:

TypeError:undefined不是Object的函数。

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版本2.0.0的Jasmine.js,Jasmine-html.js和boot.js 以及 Jasmine-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',
}

我已将我的spec文件定义为:

 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(&#34; {SOME_DIV_ID}&#34;)?

1 个答案:

答案 0 :(得分: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}");
               });  
           });                        
     });
});

我希望这会有所帮助。