【问题标题】:Testing a custom method for $state.go测试 $state.go 的自定义方法
【发布时间】:2017-01-13 03:45:30
【问题描述】:

我试图测试这段代码:

redireccion() {
this.$state.go('modifyLine', {lineId: this.look()._id});
}

look() {
return Entries.findOne({name: this.entry.name});
}

上面的代码方法没问题(看),但是对于“redirectcion”,我尝试了类似的方法,但出现错误。

这是代码:

    describe('redireccion()', () => {
      beforeEach( inject(($state) => {
      spyOn($state, 'go');
      spyOn(controller, 'look');
      spyOn(Entries, 'findOne');
      }));

    it('should be a ... bla bla', () => {
    controller.redireccion();
    expect($state.go).toHaveBeenCalledWith('modifyLine', {lineId: });
    });
   });

这是摘录,因为我真的不知道如何测试。

【问题讨论】:

    标签: javascript angularjs meteor jasmine angular-mock


    【解决方案1】:

    我会尽力为您提供见解。您应该尝试使您的测试隔离......这意味着如果您正在测试重定向,您可以模拟 look 方法,因为它不相关(对于这个特定的测试)。

     describe('testing redirection()', () => {
           beforeEach( inject(($state) => {
                //here I'm saying that I'm spying every call to $state.go
                spyOn($state, 'go');
    
                //And here I'm that I'm not only spying every call to 
                //controller.look() but I'm also replacing the original
                //implementation with my fake one. Every call will basically 
                //return an object with id equals 10
                spyOn(controller, 'look').and.callFake(() => {
                     var mockedLine = {
                         _id: 10
                     };
                     return mockedLine;
                });
           }));
    
           it('should call state.go', () => {
                controller.redireccion();
    
                //if you just want to check if the method was called, do the following:
                expect($state.go).toHaveBeenCalled();
    
                //if you need to also check the arguments, try:
                var args = $state.go.mostRecentCall.args;
                expect(args[0]).toBe('modifyLine');
                expect(args[1].lineId).toBe(10);
           });
      });
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2014-07-24
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      相关资源
      最近更新 更多