【问题标题】:Unit test typescript with jasmine and chutzpah in VS 2015VS 2015 中带有 jasmine 和 chutzpah 的单元测试打字稿
【发布时间】:2017-05-14 09:20:32
【问题描述】:

我是单元测试的新手,我正在使用带有 typescript 的 angularjs。对于单元测试,我使用 jasmine 和 chutzpah,但遇到错误“ReferenceError: Can't find variable: angular

测试文件如

//File:test.ts
///<chutzpah_reference  path="../Scripts/angular.js"/>
///<chutzpah_reference  path="../Scripts/angular-mocks.js"/>
///<chutzpah_reference  path="../Scripts/jasmine/jasmine.js"/>
///<reference path="../scripts/typings/angularjs/angular.d.ts"/>
///<reference path="../scripts/typings/angularjs/angular-mocks.d.ts"/>
///<reference path="../scripts/typings/jasmine/jasmine.d.ts"/>
///<reference path="../controller/testController.ts"/>

module app {
    describe("testwithjasmine", () => {
        var testController: Testcontroller;
        beforeEach(()=> {
            testController = new Testcontroller();
        })
        //This test OK
        it("Test controller", () => {
            expect(testController.tilte).toBe("title");
        })
    })

    //Error with case below
    describe("testInject", () => {
        var scope, controller;
        beforeEach(angular.mock.module("app"));
        beforeEach(() => inject(($controller, $scope) => {
            scope = {};
            controller = $controller('testController', { $scope: scope });
        }));
        it("test contrller2", () => {
            expect(scope.title).toEqual("title");
        })
    })
}

chutzpah.json 内容

{
"Compile": {
    "Mode": "External",
    "Extensions": [".ts"],
    "ExtensionsWithNoOutput": [".d.ts"]
   },
  "References": [
    {
      "Includes": [
        "*/Controller/*.ts"
      ],
      "Excludes": [
        "*/Scripts/typings/jasmine/*.d.ts",
        "*/scripts/typings/angularjs/*.d.ts"
      ]
    }
  ],
  "Tests": [
    {
      "Includes": [ "*/Test/*.ts" ],
      "Excludes": [
        "*/Scripts/Tests/*.d.ts"
      ]

    }
  ]
}

有什么建议吗? 谢谢!

【问题讨论】:

  • 尝试在"*/Controller/*.ts"之后添加另一行:*/Scripts/angular.js
  • 感谢@SlavaUtesinov 我添加了“*/Scripts/angular.js”但不起作用。

标签: angularjs unit-testing typescript jasmine chutzpah


【解决方案1】:

chutzpah_reference 当你的测试用打字稿编写时不起作用(返回空内容)。

我最终将我的 *.js 文件托管在一个单独的 Web 应用程序中(比如 localhost:9090),还创建了自定义 harnass 文件(VS > R# > Options > Tools > Unit testing > JavaScript Tests)并将脚本标记添加到包括库脚本。

<script src="http://localhost:9090/lib/angular.js" ></script>

任何使用 R# 的尝试都失败了,例如由于各种限制,commonjs 将无法工作(即使您使用自定义工具也无济于事)。

我在 VS(但没有 R#)中知道的最佳替代方法是使用包含所有脚本的自定义 html 文件并“手动”托管文件。

就我个人而言,我正在寻找其他不需要 resharper 甚至 VS 的替代方案。

【讨论】:

  • 谢谢@Ibrahim ben Salah,你能给我一个关于在打字稿中测试控制器的例子吗?我找不到任何工作教程。
猜你喜欢
  • 2018-09-06
  • 2013-11-13
  • 1970-01-01
  • 2016-07-18
  • 2017-06-21
  • 2017-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多