2013-10-08 31 views
29

我设置了我的SpecRunner.html/.js,RequireConfig.js,我的路径和垫片,就像我在Jasmine + RequireJs的早期版本中一样,但是现在我的测试方法显示Jasmine未定义。他们最近更改为另一种加载Jasmine的方法,我知道它与RequireJs不兼容。Jasmine 2.0真的不能与require.js一起使用吗?

我的理解是否正确?如果是这样,我们能否再次使用Jasmine + RequireJs?

回答

68

新的boot.js完成了一堆初始化,并将其附加到window.onload(),该时间段已被require.js加载Jasmine调用。您可以手动调用window.onload()来初始化HTML Reporter并执行该环境。

SpecRunner.html

<!doctype html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Jasmine Spec Runner v2.0.0</title> 

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css"> 

    <!-- specRunner.js runs all of the tests --> 
    <script data-main="specRunner" src="../bower_components/requirejs/require.js"></script> 
    </head> 
    <body> 
    </body> 
</html> 

specRunner.js

(function() { 
    'use strict'; 

    // Configure RequireJS to shim Jasmine 
    require.config({ 
    baseUrl: '..', 
    paths: { 
     'jasmine': 'tests/lib/jasmine-2.0.0/jasmine', 
     'jasmine-html': 'tests/lib/jasmine-2.0.0/jasmine-html', 
     'boot': 'tests/lib/jasmine-2.0.0/boot' 
    }, 
    shim: { 
     'jasmine': { 
     exports: 'window.jasmineRequire' 
     }, 
     'jasmine-html': { 
     deps: ['jasmine'], 
     exports: 'window.jasmineRequire' 
     }, 
     'boot': { 
     deps: ['jasmine', 'jasmine-html'], 
     exports: 'window.jasmineRequire' 
     } 
    } 
    }); 

    // Define all of your specs here. These are RequireJS modules. 
    var specs = [ 
    'tests/spec/routerSpec' 
    ]; 

    // Load Jasmine - This will still create all of the normal Jasmine browser globals unless `boot.js` is re-written to use the 
    // AMD or UMD specs. `boot.js` will do a bunch of configuration and attach it's initializers to `window.onload()`. Because 
    // we are using RequireJS `window.onload()` has already been triggered so we have to manually call it again. This will 
    // initialize the HTML Reporter and execute the environment. 
    require(['boot'], function() { 

    // Load the specs 
    require(specs, function() { 

     // Initialize the HTML Reporter and execute the environment (setup by `boot.js`) 
     window.onload(); 
    }); 
    }); 
})(); 

例规范

define(['router'], function(router) { 
    'use strict'; 

    describe('router', function() { 
    it('should have routes defined', function() { 
     router.config({}); 
     expect(router.routes).toBeTruthy(); 
    }); 
    }); 
}); 
+0

谢谢你的回答。我花了一个下午试图让这个工作。 – keepitreal

+0

我只是偶然发现了这个,它效果很棒!我甚至通过改变'boot.js'文件并将它加载到我选择的div中来将它附加到视图中。 – Josh

+1

伙计,非常感谢!我永远摆弄着,这让我立即开始。 – medoingthings

13

这里是一个替代方法,它可以在某些情况下更简单 - 使用Jasmine's asynchronous support加载您AMD模块在执行测试之前,如下所示:

MySpec.js

describe('A suite', function() { 
    var myModule; 

    // Use require.js to fetch the module 
    it("should load the AMD module", function(done) { 
    require(['myModule'], function (loadedModule) { 
     myModule = loadedModule; 
     done(); 
    }); 
    }); 

    //run tests that use the myModule object 
    it("can access the AMD module", function() { 
    expect(myModule.speak()).toBe("hello"); 
    }); 
}); 

对于这个工作,你需要在你的SpecRunner.html require.js并可能configure require(像往常一样,例如通过设置的baseUrl),像这样:

SpecRunner.html

<!DOCTYPE HTML> 
<html> 
    <head> 
    <meta charset="UTF-8"> 
    <title>Jasmine Spec Runner v2.0.0</title> 

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.0.0/jasmine_favicon.png"> 
    <link rel="stylesheet" type="text/css" href="lib/jasmine-2.0.0/jasmine.css"> 

    <script src="lib/require.min.js"></script> 
    <script> require.config({ baseUrl: "src" }); </script> 

    <script src="lib/jasmine-2.0.0/jasmine.js"></script> 
    <script src="lib/jasmine-2.0.0/jasmine-html.js"></script> 
    <script src="lib/jasmine-2.0.0/boot.js"></script> 

    <script src="spec/MySpec.js"></script> 

    </head> 
    <body> 
    </body> 
</html> 

在这个例子中,AMD模块实现可能是这个样子:

的src/Mymodule中.js文件

define([], function() { 
    return { 
    speak: function() { 
     return "hello"; 
    } 
    }; 
}); 

这里有一个working Plunk that implements this complete example

享受!

+0

你的Plunker是一颗宝石!谢谢 – HockeyJ

+1

或者,您也可以在beforeEach()函数中使用done(),以便它在每个规范之前运行。然后,您可以单独运行每个规格而不需要运行模块加载规范。 –

相关问题