javascript - 如何使用 resharper testr jasmin 测试 spa js 模块?

标签 javascript unit-testing requirejs resharper

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

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

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

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

因此,给定一个在 datacontext.js 中具有依赖项的 people.js View 模型模块:

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");
 });
});

最佳答案

试试这个:

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

关于javascript - 如何使用 resharper testr jasmin 测试 spa js 模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18234707/

相关文章:

javascript - Rails 4 jQuery/Ajax 在创建后刷新并将项目添加到部分

java - Infinispan缓存在悲观模式下不会抛出异常

java - 我应该对哪些方法进行 JUnit 测试 - 如何模拟具有许多依赖项的方法

javascript - 将 require js 与 angular js 一起使用

javascript - 如何让这个 ajax 下拉选择器适用于所有浏览器?

javascript - 正确捕获异步函数node.js

unit-testing - grails-测试delete()

javascript - 是否已经存在用于支持 Dojo AMD、NodeJS 要求和浏览器窗口的代码。[something] 用于 Javascript 微型库

json - 如何从单独的 js 文件中获取 json 数据到主 js 文件

javascript - 如何安全地评估用户输入表达式