找不到变量:require - 使用Chutzpah进行茉莉花单元测试

时间:2016-07-29 00:43:25

标签: unit-testing angular jasmine chutzpah

我是Churtzpah和Jasmine的新手。我一直在尝试使用Chutzpah进行测试。我正在使用Jasmine,Typescript,Chutzpah,angular2来编写单元测试。 每当我尝试运行测试时,我都会看到我的测试被检测到。 我的chutzpah.json文件如下:

     {
  "Framework": "jasmine",

  "Compile": {
    "Mode": "Executable",
    "Executable": "../compile.bat",
    "Extensions": [ ".ts" ],
    "ExtensionsWithNoOutput": [ ".d.ts" ],
    "UseSourceMaps": true,
    "Paths": [
      {
        "SourcePath": "../",
        "OutputPath": "../"
      }
    ]
  },
  "References": [
   { "Path": "../node_modules/es6-shim/es6-shim.js" },
    { "Path": "../node_modules/reflect-metadata/Reflect.js" },
    { "Path": "../node_modules/systemjs/dist/system.src.js" },
    { "Path": "../node_modules/zone.js/dist/zone.js" },
    { "Path": "../node_modules/zone.js/dist/jasmine-patch.js" },
    { "Path": "../node_modules/zone.js/dist/async-test.js" },
    { "Path": "../node_modules/zone.js/dist/fake-async-test.js" },
    { "Path": "../node_modules/core-js/client/shim.min.js" },
    {
      "Path": "app",
      "Includes": [ "*.ts" ],
      "Excludes": [ "*.d.ts" ]
    }
  ],
  "Tests": [
    { "Include": "*.spec.ts" }

  ]
}







import {it, describe, beforeEach, inject, beforeEachProviders} from "@angular/core/testing";

import {LoginService} from "./login.service";

describe("testing LoginService", () => {
    let Myservice: LoginService = new LoginService();
    it("Should return true", () => {
        expect(Myservice.validateUser("test", "test")).toBe(true);
    });
        it("validateUser should fail if input values are null", () => {
            expect(Myservice.validateUser(null, null)).toBe(false);
        });
    });

让我知道我还需要做些什么。 感谢

compile.bat看起来像

    @echo off tsc.cmd app/login/login.service.ts app/tests/login.service.spec.ts --sourcemap--declaration

我在测试输出窗口中看到以下错误

Error: ReferenceError: Can't find variable: require
at global code in file:"path"
0 passed, 0 failed, 0 total (chutzpah).

========== Total Tests: 0 passed, 0 failed, 0 total ==========

2 个答案:

答案 0 :(得分:1)

在Chutzpah回购中,有一个Angualr-Typescript样本可能有助于获得you started。下面是Chutzpah.json,但需要注意几个关键事项。

  1. 确保测试未包含在参考部分
  2. 确保在右侧
  3. 中单独明确引用关键角度框架文件

    Chutzpah.json

    {
      "Framework": "jasmine",
      "Compile": {
        "Mode": "Executable",
        "Executable": "../compile.bat",
        "Extensions": [ ".ts" ],
        "ExtensionsWithNoOutput": [ ".d.ts" ],
        "UseSourceMaps": true,
        "Paths" : [
          { "SourcePath": "../", "OutputPath": "../" }
        ]
      },
      "References": [
        { "Path": "../../libs/angular.min.js", "IsTestFrameworkFile": true },
        { "Path": "../../libs/angular-mocks.js", "IsTestFrameworkFile": true },
        { "Path": "../src", "Includes": [ "*.ts" ], "Excludes": ["*.d.ts"] }
      ],
      "Tests": [{ "Include": "*Tests.ts" }]
    }
    

答案 1 :(得分:1)

看起来您的源路径有错误。你指的是:

"Path": "../app/"

但是这条路径是相对于chutzpah.json文件的位置。它应该说:

"Path": "app"