首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Jasmine和RequireJS的Squire.js示例

使用Jasmine和RequireJS的Squire.js示例
EN

Stack Overflow用户
提问于 2013-09-30 16:19:26
回答 1查看 4.7K关注 0票数 20

我想写JS测试。生产代码是用RequireJS编写的。我找到了一个名为Squire.js:https://github.com/iammerrick/Squire.js/的测试库

来自Squire.js网站

Run生成一个函数,该函数将接收完成的回调,并在测试函数完成后执行它。对于使用回调处理异步的框架特别有用。下面是一个使用Mocha.js的示例。Jasmine可以使用Jasmin.Async提供这种回调方法。“

我不知道如何将此与Jasmine async一起使用。一个小的例子会非常有用。

EN

回答 1

Stack Overflow用户

发布于 2014-01-17 04:33:00

这是一个很好的设置,用于将带有模拟依赖项的模块插入到测试中。这是一个端到端的示例,如果您只想查看规范,请跳到最后。

文件夹结构:

代码语言:javascript
复制
Jasmine
|-lib
||-jasmine          -- Contains all the Jasmine files
|||-boot.js
|||-jasmine-html.js
|||-jasmine.js
|||-jasmine.css
|||-jasmine_favicon.png
|||-...
|-spec              -- Spec files go here
||-hello-spec.js
|SpecRunner.html
|SpecRunner.js
|squire.js
Scripts
|knockout.js
|require.js
|jquery.js
App
|-hello.js
|-foo.js

在您提出问题时,Jasmine 1.3是最新版本。从那时起,2.0发布了,并包含了一些异步改进。这两个版本都在这里,1.3被注释掉了。

SpecRunner.html:

代码语言:javascript
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Hello Spec Runner</title>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine/jasmine_favicon.png">
    <link rel="stylesheet" type="text/css" href="lib/jasmine/jasmine.css">
</head>
<body>
    <!-- Load RequireJS & the testsuite -->
    <script src="../Scripts/require.js" data-main="SpecRunner.js" type="text/javascript" ></script>
</body>
</html>

SpecRunner.js:

这个问题Does Jasmine 2.0 really not work with require.js?对启动和运行它很有帮助。

代码语言:javascript
复制
(function () {
    'use strict';

    // Configure RequireJS to shim Jasmine
    requirejs.config({
        baseUrl: "../App",
        paths: {
            'jasmine' : '../Jasmine/lib/jasmine/jasmine',
            'jasmine-html': '../Jasmine/lib/jasmine/jasmine-html',
            'boot': '../Jasmine/lib/jasmine/boot', // This is not present in Jasmine 1.3
            'spec' : '../Jasmine/spec',
            'squire': '../Jasmine/squire',
            'knockout': '../Scripts/knockout-2.3.0',
            'jquery': '../Scripts/jquery-1.10.2' // This only used in the Jasmine 1.3 case.
        },
        shim: {
            'jasmine': {
                exports: 'jasmine'
            },
            'jasmine-html': {
                deps: ['jasmine'],
                exports: 'jasmine'
            },
            'boot': {
                deps: ['jasmine', 'jasmine-html'],
                exports: 'jasmine'
            },
            "squire": {
                exports: "squire"
            }
        }
    });

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

    // 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();
        });
    });

    /******
     * Use this require if you're on Jasmine 1.3
     ******/
    //require(['jquery', 'jasmine-html'], function ($, jasmine) {
    //  var jasmineEnv = jasmine.getEnv();
    //  jasmineEnv.updateInterval = 1000;

    //  var htmlReporter = new jasmine.HtmlReporter();

    //  jasmineEnv.addReporter(htmlReporter);

    //  jasmineEnv.specFilter = function(spec) {
    //      return htmlReporter.specFilter(spec);
    //  };

    //  $(function() {
    //      require(specs, function(spec) {
    //          jasmineEnv.execute();
    //      });
    //  });
    //});

    // end 1.3
})();

foo.js:

代码语言:javascript
复制
define(['knockout'], function (ko) {

    var message = ko.observable("Go away, World!");

    return {
        message: message()
    };
});

hello.js:

代码语言:javascript
复制
define(['foo'], function (foo) {

    return {
        message : foo.message
    };

});

hello-spec.js:

代码语言:javascript
复制
define(['squire'], function (Squire) {

    var injector = new Squire();
    var builder = injector
        .mock('foo', {
                message: "Hello, World!"
        });

    describe('hello', function () {
        var hello;

        beforeEach(function (done) {
            // For async:
            // accept a "done" parameter and Jasmine will wait...
            builder.require(['hello'], function (hi) {
                hello = hi;
                done(); // ...until you invoke it.
            });

            /*******
             * Use the following if you're on 1.3
             *********/
            //var done;
            //runs(function () {
            //    builder.require(['hello'], function (hi) {
            //        hello = hi;
            //        done = true;
            //    });
            //});

            //waitsFor(function () {
            //    return done;
            //}, "Unable to load dependency not loaded", 750);

            // end 1.3
        });

        it('is welcoming', function() {
            expect(hello.message).toBe("Hello, World!");
        });
    });

    describe('hello no mock foo', function () {
        var hello;

        beforeEach(function (done) {
            // For async:
            // accept a "done" parameter and Jasmine will wait...
            require(['hello'], function (hi) {
                hello = hi;
                done(); // ...until you invoke it.
            });

            /*******
             * Use the following if you're on 1.3
             *********/
            //var done;
            //runs(function () {
            //    require(['hello'], function (hi) {
            //        hello = hi;
            //        done = true;
            //    });
            //});

            //waitsFor(function () {
            //    return done;
            //}, "Unable to load dependency not loaded", 750);

            // end 1.3
        });

        it('is less than welcoming', function () {
            expect(hello.message).toBe("Go away, World!");
        });
    });
});

详细信息

线条

代码语言:javascript
复制
var injector = new Squire();
var builder = injector
    .mock('foo', {
        message: "Hello, World!"
    });

模拟hello对foo的依赖,然后

代码语言:javascript
复制
builder.require(['hello'], function (hi) {
    hello = hi;
    done(); // ...until you invoke it.
});

使用模拟的foo加载hello模块。与在第二个测试中使用require本身加载hello进行比较:

代码语言:javascript
复制
 require(['hello'], function (hi) {
     hello = hi;
     done(); // ...until you invoke it.
 });

Jasmine文档http://jasmine.github.io/可以填写“完成()”功能(2.0)或“运行/等待”(1.3)。

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19089472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档