我正在尝试使用MockBackend为我的服务编写单元测试,每次我都得到一个未定义的响应。任何帮助,将不胜感激。 我已经检查了下面的所有这些解决方案,并与我的相比,我真的没有看到太多差异。
这是我的服务:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { SomeObject } from './someObject';
@Injectable()
export class SomeService {
private serviceUrl: string = 'http://localhost:8080/getObjects';
constructor( private http: Http ) { }
getObjects() {
return this.http.get( this.serviceUrl )
.map(( response ) => response.json().content as SomeObject[])
}
}
这是我的测试:
import { Http, BaseRequestOptions, Response, ResponseOptions } from '@angular/http';
import { TestBed, tick, fakeAsync, inject } from '@angular/core/testing';
import { MockBackend } from '@angular/http/testing';
import { SomeService } from './some.service';
import { SomeObject } from './someObject';
describe( 'SomeServiceTest', () => {
let stubData: SomeObject[] = [
new SomeObject( 1, "Title" )
];
beforeEach(() => {
TestBed.configureTestingModule( {
providers: [
SomeService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: ( backend: MockBackend, options: BaseRequestOptions ) => {
return new Http( backend, options );
},
deps: [MockBackend, BaseRequestOptions]
}
]
});
});
it( 'should fetch data', inject( [SomeService, MockBackend], fakeAsync(( service: SomeService, mockBackend: MockBackend ) => {
let res: SomeObject[];
mockBackend.connections.subscribe( c => {
expect( c.request.url ).toBe( 'http://localhost:8080/getObjects' );
c.mockRespond( new Response( new ResponseOptions( {
body: JSON.stringify( stubData )
}) ) );
});
service.getObjects().subscribe(( response ) => {
console.log( "response :: " + JSON.stringify( response ) ); // this is where I am seeing undefined
res = response;
});
tick();
expect( res[0].name ).toBe( 'Title' ); // this returns an exception TypeError: Cannot read property '0' of undefined
}) ) );
});
答案 0 :(得分:4)
我可能错了,但我相信您的问题首先是您从服务中返回 class TransarentPanel : Panel {
public bool drag = false;
public bool enab = false;
private int m_opacity = 100;
private int alpha;
System.Drawing.Font drawFont;
System.Drawing.SolidBrush drawBrush;
System.Drawing.StringFormat drawFormat;
public TransarentPanel() {
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.Opaque, true);
this.BackColor = Color.Transparent;
}
public int Opacity {
get {
if (m_opacity > 100) {
m_opacity = 100;
} else if (m_opacity < 1) {
m_opacity = 1;
}
return this.m_opacity;
}
set {
this.m_opacity = value;
if (this.Parent != null) {
Parent.Invalidate(this.Bounds, true);
}
}
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle = cp.ExStyle | 0x20;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle bounds = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
Color frmColor = this.Parent.BackColor;
Brush bckColor = default(Brush);
alpha = (m_opacity * 255) / 100;
if (drag) {
Color dragBckColor = default(Color);
if (BackColor != Color.Transparent) {
int Rb = BackColor.R * alpha / 255 + frmColor.R * (255 - alpha) / 255;
int Gb = BackColor.G * alpha / 255 + frmColor.G * (255 - alpha) / 255;
int Bb = BackColor.B * alpha / 255 + frmColor.B * (255 - alpha) / 255;
dragBckColor = Color.FromArgb(Rb, Gb, Bb);
} else {
dragBckColor = frmColor;
}
alpha = 255;
bckColor = new SolidBrush(Color.FromArgb(alpha, dragBckColor));
} else {
bckColor = new SolidBrush(Color.FromArgb(alpha, this.BackColor));
}
if (this.BackColor != Color.Transparent | drag) {
g.FillRectangle(bckColor, bounds);
}
// Display overlay info
{
drawFont = new System.Drawing.Font("Arial", 24);
drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
drawFormat = new System.Drawing.StringFormat();
g.DrawString("Overlay", drawFont, drawBrush, this.Location.X + 50, this.Location.Y + 30, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
drawFormat.Dispose();
}
bckColor.Dispose();
g.Dispose(); // Should this object be disposed of here since it was not created here?
base.OnPaint(e);
}
protected override void OnBackColorChanged(EventArgs e)
{
if (this.Parent != null)
{
Parent.Invalidate(this.Bounds, true);
}
base.OnBackColorChanged(e);
}
protected override void OnParentBackColorChanged(EventArgs e)
{
this.Invalidate();
base.OnParentBackColorChanged(e);
}
} // end class TransparentPanel
。response.json().content as Object[]
属性很可能未定义。我建议只将你的响应映射到一个json对象并让你的组件处理它。
content
然后
getObjects() {
return this.http.get( this.dealsUrl )
.map(( response ) => response.json());
}
希望这有帮助