代码之家  ›  专栏  ›  技术社区  ›  physicsboy

监视变量值-角度2+

  •  0
  • physicsboy  · 技术社区  · 7 年前

    是否可以监视变量的值?

    我想检查一个变量的值在我执行一个函数之后是否发生了变化,即:

    阿普茨

    export class AppComponent {
        var someVar = '';
    
        myfunct() {
          this.someVar = 'hello world';
        }
    }
    

    App.T.TS

    let component: AppComponent
    
    beforeEach(() => {
        TestBed.configureTestingModule({
          declarations: [AppComponent],
          imports: []
        }).compileComponents();
    
        fixture = TestBed.createComponent(AppComponent);
        component = fixture.componentInstance;
    
    it('should equal hello world', () => {
      component.myFunct();
      expect(component.someVar).toEqual('hello world');
    
    });
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   yoerids    7 年前

    export class ExampleComponent {
        public someVar: string;
    
        constructor() {
           this.someVar = "";
        }
    
        public someFunction() {
           this.someVar = "Hello World";
        }
    }
    

     describe("ExampleComponent", () => {
       let component: ExampleComponent;
       describe("When the component is initialized", () => {
          beforeEach(() => {
            component = new ExampleComponent();
          });
    
          it("should have a variable someVar that is empty"), () => {
            expect(component.someVar).toEqual("");
          });
    
          describe("And when someFunction is called", () => {
            beforeEach(() => {
                component.someFunction();
            });
    
            it("should have a variable someVar that is 'Hello World'"), () => {
                expect(component.someVar).toEqual("Hello World");
            });
        });
      });
    });
    
    推荐文章