1

在阅读了 VS 中的 javascript 单元测试/bdd 之后,我发现您可以使用以下组合:

- ReSharper - support for PhantomJS headless + Jasmine/QUnit
- Testr - mock Require dependencies

我在测试脚本中使用了 Jasmine,并且能够成功运行一些简单的测试,并在同一个文件中声明了函数。

但是,我找不到/构建一个端到端的工作示例来测试具有依赖关系的 js 模块。我正在尝试以 John Papa 在 SPA Jumpstart 示例中使用的示例为基础。

因此,给定一个在 datacontext.js 中具有依赖关系的 people.js 视图模型模块:

define(['services/datacontext'],
 function (datacontext) {
var peopleViewModel = {        
                       title: 'People page'
                      };
return peopleViewModel;
})

文件夹结构:

/App/Viewmodels : people.js
/App/Services : datacontext.js
/App/Tests : peopletests.js

我需要在 peopletests.js 中添加什么才能运行此测试?

describe("My Tests Set", function () {
 it("People Title Test", function () {
   expect(peopleViewModel.title()).toEqual("People page");
 });
});
4

1 回答 1

1

尝试这个:

  1. 添加 require.js 作为参考路径
  2. 添加 require.config 脚本作为参考路径
  3. 加载需要模块。

peopletests.js:

/// <reference path="~/Scripts/require.js"/>
/// <reference path="~/App/requireConfig.js"/>

describe("My Tests Set", function () {
var people;

beforeEach(function () {
    if (!people) { //if people is undefined it will try to load it
       require(["people"], function (peopleViewModel) {
            people = peopleViewModel;
        });
        //waits for people to be defined (loaded)
        waitsFor(function () { 
            return people;
        }, "loading external module", 1000);
    }
});

 it("People Title Test", function () {
   expect(people.title).toEqual("People page");
 });
});

要求配置.js:

//beware of the port, if your app is runing in port 8080 
//you need to specified that to require since resharper whould use a random port 
//when running tests 
require.config({
    baseUrl: 'http://localhost:8080/App/',
    paths: {
        people: 'ViewModels/people',
        dataContext: 'Services/datacontext'
    }
});

人.js

define(['dataContext'],
 function (datacontext) {
var peopleViewModel = {        
                       title: 'People page'
                      };
return peopleViewModel;
})
于 2014-01-16T02:28:14.753 回答