【问题标题】:How to test spa js modules with resharper testr jasmin?如何使用 resharper testr jasmin 测试 spa js 模块?
【发布时间】:2013-08-14 14:37:21
【问题描述】:

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

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

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

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

因此,给定一个 people.js 视图模型模块,该模块在 datacontext.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");
 });
});

【问题讨论】:

  • 我目前正在处理同样的问题。如果能够配置 resharper 的 jasmine 测试运行器以与 requirejs 一起工作,那就太好了。

标签: javascript unit-testing requirejs resharper


【解决方案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");
 });
});

requireConfig.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'
    }
});

people.js

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

【讨论】:

    猜你喜欢
    • 2017-11-22
    • 1970-01-01
    • 2013-09-06
    • 2014-05-11
    • 2013-04-19
    • 2010-12-31
    • 2020-08-22
    • 1970-01-01
    • 2016-10-15
    相关资源
    最近更新 更多