我有 myComponent 其中包含method1
,method2
和ngOnInit
.
export class myComponent {
//input and output declaration
public myVar;
constructor( @Inject(ElementRef) private elementRef: ElementRef) {
}
public method1() { return this.myVar.val().toUpperCase(); }
public method2() { return this.myVar .val(""); }
public ngOnInit() {
this.myVar = //calling jQuery autocomplete method which in turns calls $.JSON () to get data .
//
}
这是该组件的 html 模板:
<input type="text" value="{{symbol}}" size="{{size}}" maxlength="94"><span></span>
这是我的规格文件。我需要确保插入的任何值都转换为大写。
describe('myComponent Component', () => {
beforeEachProviders(() => [myComponent, provide(ElementRef, { useValue: new MockElementRef() })]);
class MockElementRef implements ElementRef {
nativeElement = {};
}
it('should check uppercase conversion',inject([TestComponentBuilder, myComponent , ElementRef], (tcb:TestComponentBuilder) => {
tcb.createAsync(myComponent)
.then((fixture) => {
const element = fixture.nativeElement.firstChild;
element.setAttribute("value", "g");
element.setAttribute("size", 12); //setting size and value for input element
var getMyElem= $(element);
let ac= new myComponent(fixture.nativeElement);
ac.ngOnInit(); //undefined
//ac.method1(); unable to call
console.log(myComponent.prototype.method1()); //it calls value method but outputs cannot read val of undefined
expect(element.getAttribute("value")).toBe("G");
});
}));
});
我希望将值“g”设置为等于“g”,并检查调用后是否返回“G” method1()
。
问题 :
1.在创建myComponent实例时传递fixture.nativeElement作为参数对吗?
2. 另外,如果您可以帮助我测试组件内部调用的 $.JSON 方法。如何模拟 JSON 请求?