Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jasmine 茉莉花:需求没有定义_Jasmine_Angular - Fatal编程技术网

Jasmine 茉莉花:需求没有定义

Jasmine 茉莉花:需求没有定义,jasmine,angular,Jasmine,Angular,我通过以下链接完成了angular2快速启动项目:。唯一不同的是我在app.component.ts中添加了codevar fs=require('js'),它可以工作。但是当我为该组件编写Jasmine单元测试时,它显示require没有定义,有人知道如何解决这个问题吗 注: 我在我的整个项目中都在使用typescript 我曾尝试添加节点类型定义,但无法修复该错误 这是我的密码: unit-tests.html <!DOCTYPE html> <html> <h

我通过以下链接完成了angular2快速启动项目:。唯一不同的是我在app.component.ts中添加了code
var fs=require('js')
,它可以工作。但是当我为该组件编写Jasmine单元测试时,它显示
require没有定义
,有人知道如何解决这个问题吗

注:

我在我的整个项目中都在使用typescript

我曾尝试添加节点类型定义,但无法修复该错误

这是我的密码: unit-tests.html

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>Ng App Unit Tests</title>
    <link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
    <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
    <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
    <script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
    ...
</head>
<body>
<!-- Unit Testing Chapter #1: Proof of life.  -->
<script src="app/app.component.spec.js"></script>
</body>
</html>

Ng应用程序单元测试
...

您需要包括SystemJS并导入您的规范

比如:

<script src="~/node_modules/systemjs/dist/system.js" type="text/javascript"></script>
<script>  
    System.config({
        baseURL: '/app'
    });
      System.import('app.component.spec')
    .then(null, console.error.bind(console));
</script>

System.config({
baseURL:“/app”
});
System.import('app.component.spec')
.then(null,console.error.bind(console));

您的代码缺少所有类成员都需要的this关键字

将您的组件更改为:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent {

  constructor() {
      this.testReq();
  }

  testReq(str: string): string {
    /*  var fs = require('fs');
      var str = '123';
      return str;*/
  }
}
从'angular2/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:“我的第一个Angular 2应用程序”
})
导出类AppComponent{
构造函数(){
this.testReq();
}
testReq(str:string):string{
/*var fs=需要('fs');
var-str='123';
返回str*/
}
}

我想你误解了这里的一些事情。FS是一个用于处理文件系统的节点库。当您处于节点进程中时,这将非常有效,但在您的示例中,情况并非如此。您正在浏览器进程中运行,因此无法访问任何节点库。

请发布规范运行程序的代码?-我假设您使用的是specrunner.html…它与网站中的unit-tests.html相同,请参阅我问题中的代码。您可以发布组件代码吗?我将为您创建一个plunk这是我的plunk:出于某种原因,它不起作用。但在我的应用程序中,我有类似的代码,它可以工作,只是在进行测试时不起作用。当然,我有比上面的代码更多的代码,例如,我使用fs读取函数testReq中的文件(它们实际上在我的应用程序中起作用,只是在Jasmine测试中不起作用)。在这个例子中,我只是简化了代码,让您知道我在哪里需要fs。