javascript - Angular 2 Http请求 promise 返回区域对象而不是实际数据

标签 javascript angularjs unit-testing http promise

我正在尝试快速测试 ng 2 http 以返回真实数据。我知道有更好/更长的方法来做到这一点。这是为了快速和简单,而不是最佳实践。

我知道服务器返回数据,因为我可以在另一个终端窗口中看到它。 json 非常简单 {a:b} 因为它只是一个概念证明。

我不在乎它是一个 promise 还是一个 observable,只要它能在那里返回真实数据——这样我就可以弄清楚它确实有效——而不是我想编写生产代码那样。

//app.data.service.ts
import { Injectable }     from '@angular/core';
import { Http, Response} from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';

@Injectable() export class DataService {
    constructor(private http: Http) { 
    }
    public getItems(){
       return this.http.get('http://localhost:8090/data/config.txt')
           .toPromise()
           .then(data => Promise.resolve(data.json()));
    }
}

// app.data.service.spec.ts
/* tslint:disable:no-unused-variable */
import { AppComponent } from './app.component';
import { TestBed, inject, fakeAsync } from '@angular/core/testing';
import { MockBackend, MockConnection } from '@angular/http/testing';
import { By } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';
import { DataService } from './app.data.service';
describe('DataService', function () {
  let dataService: DataService;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpModule],
      declarations: [AppComponent],
      providers: [DataService]
    });
    dataService = TestBed.get(DataService);
  });
  it('should be instantiated by the testbed', () => {
    expect(dataService).toBeDefined();
  });
  it('should return get', () => {
    let data = dataService.getItems();
    console.log('test data= ' + data);
    console.log('test string(data)= ' + JSON.stringify(data));
  });
});

//tail end of tests.html
      <tr class="system-out">
        <td colspan="3"><strong>System output:</strong><br />Chrome 53.0.2785 (Mac OS X 10.11.6) LOG: 'WARNING: System.import could not load "systemjs.config.extras.js"; continuing without it.'
<br />Chrome 53.0.2785 (Mac OS X 10.11.6) LOG: Error{originalErr: Error{}}
<br />Chrome 53.0.2785 (Mac OS X 10.11.6) LOG: 'test data= [object Object]'
<br />Chrome 53.0.2785 (Mac OS X 10.11.6) LOG: 'test string(data)= {"__zone_symbol__state":null,"__zone_symbol__value":[]}'
</td>

最佳答案

In app.data.service.ts

public getItems(){
   return this.http.get("http://......")
       .toPromise()
       .then(res => res.json())
       .catch(this.handleError);
}

In your component.ts call this method/subscribe to it

  data:any;

  ngOnInit() {
    this.appService.getItems()
        .then(data => console.log(data));

  }

关于javascript - Angular 2 Http请求 promise 返回区域对象而不是实际数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39681910/

相关文章:

javascript - 如何在 Angular 2 或 4 的 Jasmine 测试中模拟鼠标事件

angularjs - $element 和 $ attrs 在带有 angularJS 组件的组件 Controller 中的用途 1.5

javascript - Node js 中的条件 promise

javascript - 如何使用 cql 过滤器过滤地理服务器图层组

javascript - 使用 RegExp 限制输入数据

javascript - AngularJs 绑定(bind)值不显示

angularjs - 最后在 promise 兑现之前立即接到电话

.net - 重定向程序集绑定(bind)是否适用于使用测试运行程序进行单元测试?

unit-testing - 执行 Go 测试下划线 (_) 代替正确的路径

javascript - TypeScript 中的 async/await 和 promise