Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular after fixture.detectChanges()中更改输入属性会导致奇怪的行为_Angular_Unit Testing - Fatal编程技术网

在Angular after fixture.detectChanges()中更改输入属性会导致奇怪的行为

在Angular after fixture.detectChanges()中更改输入属性会导致奇怪的行为,angular,unit-testing,Angular,Unit Testing,假设有两个组件:AppComponent和TestComponent。我使用AppComponent的HTML模板中的指令调用TestComponent。现在TestComponent有一个@Input()属性(让它成为myTitle) 我只对TestComponent进行单元测试。对于title,我在测试中传递了一个随机值。以下是相同的代码: app.component.html <span><app-test [myTitle]="title"></app-te

假设有两个组件:AppComponent和TestComponent。我使用AppComponent的HTML模板中的指令调用TestComponent。现在TestComponent有一个@Input()属性(让它成为myTitle)

我只对TestComponent进行单元测试。对于title,我在测试中传递了一个随机值。以下是相同的代码:

app.component.html

<span><app-test [myTitle]="title"></app-test></span>
test.component.html

<p>test works!!{{myTitle.name}}</p>
测试组件规范ts

describe('Test component',() =>{
    let temp;
    let component: TestComponent;
    let fixture: ComponentFixture<TestComponent>;

    beforeEach(async(() =>{
       TestBed.configureTestingModule({
           declarations: [TestComponent],
           schemas: [NO_ERRORS_SCHEMA]
       }) 
       .compileComponents();
    }));

    beforeEach(()=>{
        temp = {name: "Heloooo"};
       fixture = TestBed.createComponent(TestComponent);
       component = fixture.componentInstance;
    });

    it('should check First',()=>{
       component.myTitle = temp;
       console.log(component.myTitle.name);
       console.log(temp.name);

       fixture.detectChanges();

       console.log(component.myTitle.name);
       console.log(temp.name);
       expect(component.myTitle.name).toEqual(temp.name);
    });

    it('should check Second',()=>{
       component.myTitle = temp;
       console.log(component.myTitle.name);
       console.log(temp.name);

       fixture.detectChanges();

       temp.name = "Majin Buu";
       console.log(component.myTitle.name);
       console.log(temp.name);
       expect(component.myTitle.name).toEqual(temp.name);
    });
});
测试用例2:

Heloooo       
Heloooo             
Majin Buu     
Majin Buu     
为什么会显示object.name的最新值?我以为这个物体

临时工

在这种情况下是局部的

  • fixture.detectChanges()是否只调用ngOnInit?如果没有,那么它是如何工作的?如何确保对象的测试用例也失败

  • 我是这个社区的新手,所以如果有任何问题失败,请帮助我改进问题。

    当您将一个对象分配给另一个对象时。仅引用不会更改该对象的堆地址

    因此这里temp是一个对象,您将其分配给myTitle,myTitle和temp都引用同一个对象意味着该对象中的任何更改都将通过myTitle和temp反映出来

    在测试用例2中,当您更改临时值时,您正在更新同样由myTitle引用的对象的值。这就是它被通过的原因

    temp: object | Array // will get change properties value
    temp:number | string | boolean // will not change.
    
  • fixture.detectChanges()并不意味着运行ngOnInit()。它用于告诉Angular运行更改检测

  • 谢谢你的回答。但是我想知道,在fixture.detectChanges()之后,我正在更新第二个测试用例中的temp对象,该更改是如何反映的?我也没有通过父母对孩子的任何测试,我只是在测试孩子。。。请回答我的最后一个问题。非常感谢。我还想知道一件事,在调用fixture.detectChanges()时,ngOnInit()内的行会根据代码覆盖率结果被调用两次。为什么会这样?它如何准确地检查变化检测,它能调用ngOnChanges()吗?它应该调用2次。在运行两个测试之后,您是否考虑?你可以从这里读到没有高拉夫,与此无关。但是与单元测试相关。。谢谢你那么你能把这个关上吗?
    Heloooo          
    Heloooo               
    Hi!!          
    Hi!!      
    
    Heloooo       
    Heloooo             
    Majin Buu     
    Majin Buu     
    
    temp: object | Array // will get change properties value
    temp:number | string | boolean // will not change.