您还可以在测试中访问window
对象。所以你可以很容易地监视他们。
我已经创建了一个包含特定用例的轻量级组件。
以下是部分:在这里我不知道该Attachment
类型是什么
import { Component } from '@angular/core';
@Component({
selector: 'app-attachment',
templateUrl: './attachment.component.html',
styleUrls: ['./attachment.component.css']
})
export class AttachmentComponent {
public openAttachment(attachment) {
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(attachment.getFile());
}
else {
let objectUrl = URL.createObjectURL(attachment.getFile());
window.open(objectUrl);
}
}
}
注意。所以我已经将这个类型的注释从参数中移除到了openAttachment
函数中。
而现在这是我的测试应该是什么样子:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AttachmentComponent } from './attachment.component';
describe('AttachmentComponent',() => {
let component: AttachmentComponent;
let fixture: ComponentFixture<AttachmentComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AttachmentComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AttachmentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be created',() => {
expect(component).toBeTruthy();
});
describe('openAttachment',() => {
let attachment;
beforeEach(() => {
attachment = { getFile: function() { return 'foo'; } };
});
it('should call `window.open` if `msSaveOrOpenBlob` is not a method present on the `window.navigator`',() => {
// Since this will probably run on Chrome, Chrome Headless or PhantomJS, if won't have a `msSaveOrOpenBlob` method on it.
// So this is the test for the else condition.
let windowOpenSpy = spyOn(window, 'open');
let returnValue = { foo: 'bar' };
let urlCreateObjectSpy = spyOn(URL, 'createObjectURL').and.returnValue(returnValue);
component.openAttachment(attachment);
expect(urlCreateObjectSpy).toHaveBeenCalledWith('foo');
expect(windowOpenSpy).toHaveBeenCalledWith(returnValue);
});
it('should call the `window.navigator.msSaveOrOpenBlob` if `msSaveOrOpenBlob` is present on the navigator object',() => {
// To cover the if condition, we'll have to attach a `msSaveOrOpenBlob` method on the window.navigator object.
// We can then spy on it and check whether that spy was called or not.
// Our implementation will have to return a boolean because that's what is the return type of `msSaveOrOpenBlob`.
window.navigator.msSaveOrOpenBlob = function() { return true; };
let msSaveOrOpenBlobSpy = spyOn(window.navigator, 'msSaveOrOpenBlob');
component.openAttachment(attachment);
expect(msSaveOrOpenBlobSpy).toHaveBeenCalledWith('foo');
});
});
});
我再次想强调的事实是,我不知道是什么附件类型为进入是。因此,在我的openAttachment
描述块的beforeEach
块中,我将它分配给一个对象,该对象包含一个名为getFile
的键,其值为最终返回字符串foo
的函数。
此外,由于您的测试默认在Chrome中运行,因此window.navigator
对象上的msSaveOrOpenBlob
函数将不会获得。所以openAttachment
describe块中的第一个测试只会覆盖else块。
虽然在第二个测试中,我们在window.navigator
对象上添加了msSaveOrOpenBlob
作为函数。所以现在它可以覆盖if
分支。所以,你可以在msSaveOrOpenBlob
函数创建一个间谍和expect
这个间谍toHaveBeenCalledWith
无论是从attachment.getFile()
方法(字符串foo
在这种情况下)返回
希望这有助于。