2016-09-05 6 views
0

ためのカスタム方法:

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

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

方法上記のコードでは、私はこのような何かを試してみましたが、私が得た「redireccion」の(外観)okですエラー。

これはコードです:本当に私はこれをテストする方法がわからないので、

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: }); 
    }); 
    }); 

これは、抜粋したものです。

答えて

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); 
     }); 
    }); 
+0

ありがとうございました!私の問題を解決する;) –

+0

あなたは大歓迎です:) –

関連する問題