2016-11-21 3 views
0

Ich versuche zu verstehen, wie man Spies in Typoskript mit Jasmine verwendet. Ich habe this documentation and this example gefunden:Verständnis von Spionen in Typoskript mit Jasmine

describe("A spy", function() { 
    var foo, bar = null; 

    beforeEach(function() { 
    foo = { 
     setBar: function(value) { 
     bar = value; 
     } 
    }; 

    spyOn(foo, 'setBar').and.callThrough(); 
    }); 

    it("can call through and then stub in the same spec", function() { 
    foo.setBar(123); 
    expect(bar).toEqual(123); 

    foo.setBar.and.stub(); 
    bar = null; 

    foo.setBar(123); 
    expect(bar).toBe(null); 
    }); 
}); 

Um Spies zu verwenden Ich habe eine Methode erstellt:

export class HelloClass { 
    hello() { 
     return "hello"; 
    } 
} 

und ich versuche, es zu Spy:

import { HelloClass } from '../src/helloClass'; 

describe("hc", function() { 
    var hc = new HelloClass(); 

    beforeEach(function() { 
    spyOn(hc, "hello").and.throwError("quux"); 
    }); 

    it("throws the value", function() { 
    expect(function() { 
     hc.hello 
    }).toThrowError("quux"); 
    }); 
}); 

aber es ergibt sich:

[17:37:31] Starting 'compile'... 
[17:37:31] Compiling TypeScript files using tsc version 2.0.6 
[17:37:33] Finished 'compile' after 1.9 s 
[17:37:33] Starting 'test'... 
F....... 
Failures: 
1) Calculator throws the value 
1.1) Expected function to throw an Error. 

8 specs, 1 failure 
Finished in 0 seconds 
[17:37:33] 'test' errored after 29 ms 
[17:37:33] Error in plugin 'gulp-jasmine' 
Message: 
    Tests failed 

Antwort

0

Sie rufen nie wirklich hc.hello und daher wird es nie werfen.

Versuchen Sie dies als Test:

it("throws the value", function() { 
    expect(hc.hello).toThrowError("quux"); 
}); 

Was hier los ist, ist, dass expect(...).toThrowError eine Funktion erwartet, dass, wenn sie aufgerufen wird, wird einen Fehler werfen. Ich bin mir sicher, dass du das wusstest, aber du bist nur in der Tatsache verstrickt, dass du in der Funktion auf Parens verzichtet hast.