小编Jea*_* A.的帖子

jasmine间接继承的方法(使用typescript)与toHaveBeenCalled()无法正常工作

我当前在使用typescript类中的调用的继承方法时遇到问题,其中toHaveBeenCalled()方法返回false,即使被调用的方法被调用.请看以下场景......

我有两个用TypeScript编写的类

class Parent() {
    buyFood() {
        // buy food
    }
}

class Husband extends Parent {
    makeDinner() {
        super.buyFood();
        // make dinner;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我对班级丈夫的测试中,我只关心测试制作晚餐的逻辑,因为超级类购买食品的逻辑在其自己的测试套件中进行测试.

因此,我的测试看起来像以下类型.

let husband:Husband = new Husband();

it('Should make a good dinner', () => {
    spyOn(husband, 'buyFood');
    husband.makeDinner();

    expect(husband.buyFood).toHaveBeenCalled();
}
Run Code Online (Sandbox Code Playgroud)

即使调用了buyFood(),断言也会失败,并显示一条错误,指出从未调用过继承自Parent类的方法的husband.buyFood().

我应该如何处理这个问题,而不必通过buyFood()方法调用断言值更改?

javascript inheritance jasmine typescript

19
推荐指数
2
解决办法
6102
查看次数

Angular 5视图超时后不更新

我在Angular 5中设置了一段时间后隐藏元素的超时:

this.showElement = true;
setTimeout(function () {
  console.log('hide');
  this.showElement = false;
}, 2000);
Run Code Online (Sandbox Code Playgroud)

但是,这不会更新视图.该console.log给我一个输出,所以超时肯定能行.

我发现在Angularjs你需要打电话$apply才能开始摘要,所以我猜我只需要找到Angular 5等效的方法.

javascript asynchronous angular

13
推荐指数
3
解决办法
3万
查看次数

Angular 2 - 使用ngOnInit生命周期钩子中使用的@input测试组件

目前我正在尝试测试一个子组件,它接受来自主机组件的输入,并在ngOnInit生命周期钩子中使用,如下面的代码.

@Component({
   selector: 'my-child-component',
   template: '<div></div>'
})
class ChildComponent implements OnInit {
    @Input() myValue: MyObject;
    transformedValue: SomeOtherObject;

    ngOnInit():void {
        // Do some data transform requiring myValue
        transformedValue = ...;
    }
}

@Component({
    template:`<my-child-component [myValue]="someValue"></my-child-component>`
})
class HostComponent {
    someValue: MyObject = new MyObject(); // how it is initialized it is not important.
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,应该如何测试ChildComponent,其中myValue需要在创建时出现,同时能够访问ChildComponent.transformedValue以进行断言.

我尝试使用像这样的Angular TestBed类创建ChildComponent

componentFixture = testBed.createComponent(LoginFormComponent)
Run Code Online (Sandbox Code Playgroud)

但是ngOnInit已经调到我打电话的地步

fixture.componentInstance.myValue = someValue;
Run Code Online (Sandbox Code Playgroud)

我也尝试创建HostComponent的一个fixture,虽然这有效,但我仍然无法访问创建的ChildComponent实例,我需要在ChildComponent.transformedValue字段上执行断言.

非常感谢帮助!

非常感谢!

javascript testbed angular

10
推荐指数
1
解决办法
1799
查看次数

将图像上传到Web Api 2使用角度5

我看了几个问题和答案,但无法解决我的问题.我的代码如下:

HTML

 <input type="file" id="file" accept="image/*" name="File" (change)="handleFileInput($event.target.files)">
 <button type="button" mat-raised-button color="primary" (click)="uploadFileToActivity()">Upload</button>
Run Code Online (Sandbox Code Playgroud)

零件

  handleFileInput(files: FileList) {
    this.fileToUpload = files.item(0);
  }

  uploadFileToActivity() {

    this._dishAddService.postFile(this.fileToUpload).subscribe(data => {

      }, error => {
        console.log(error);
      });
  }
Run Code Online (Sandbox Code Playgroud)

服务

 postFile(fileToUpload: File): Observable<boolean> {
    const formData: FormData = new FormData();
    formData.append('fileKey', fileToUpload, fileToUpload.name);
    let headers = new Headers({ 'Content-Type': 'application/json' });    
    headers.append('Content-Type', 'multipart/form-data');

    let options = new RequestOptions({ headers: headers, method: 'post' });
    return this.http.post(this.httpRequestUrl + 'api/dish/UploadDishImage', formData, options)
      .map(
        (response => response.json()))
      .catch(CommonFunctionService.handleError);
  }
Run Code Online (Sandbox Code Playgroud)

API …

form-data asp.net-web-api2 angular

7
推荐指数
1
解决办法
5741
查看次数