Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

angular - 如何在特定测试级别上禁用变更检测(how to disable change detection on a specific test level)

I have a component that resets certain variables to null on ngOnInit.

(我有一个组件,可以将ngOnInit上的某些变量重置为null。)

(Note that if I am move the variable resetting to the constructor of the component, it works great but since I didn't write the component in the first place, I am curious to see if changeDetection can be turned off on a test by test case from the spec file.) That is affecting one specific test which needs a variable to pass but the variable is reset to null.

((请注意,如果我将变量重置移动到组件的构造函数中,则效果很好,但是由于我没有首先编写组件,因此我很想知道是否可以在每次测试中关闭changeDetection这会影响一个特定的测试,该测试需要通过变量,但该变量会重置为null。)

What I am hoping to get is in my test case to turn of fixture.detectChanges() but still set variable values from it that can be picked or that is not possible?

(我希望得到的是在测试用例中转到Fixture.detectChanges(),但仍然从中设置可以选择或不可能的变量值?)

component:

(零件:)

export class TravelRequestComponent implements OnInit {

 fieldType : number = null;
 ngOnInit(){
 this.fieldType = null;

    }

setFieldType(fieldType){
  this.fieldType = fieldType
 }
processForm(){
    console.log(this.fieldType); //null all the time
   if(this.fieldType){
      //process form
  }
  else{
   this.toastr.error("","Select Field Type");
  }

 }

 }

setFieldType is called from a dropdown element.

(setFieldType是从下拉元素中调用的。)

 beforeEach(() => {
  fixture = TestBed.createComponent(TravelRequestComponent);
  component = fixture.componentInstance;

//implementation
 fixture.detectChanges();
 });


it("should return the same user", ()=>{

  component.countiesList = MockData.Counties;
  component.fieldType = 1;
  fixture.detectChanges();
  component.processForm();
  });
  ask by Wede Asmera Tseada translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...