string.match や string.replace などをテストするためにスパイは必要ないことがわかりましたが、一致または置換する内容を含むテキストを宣言し、beforeEach で関数を呼び出してから、応答を確認します。あなたが期待するものと同じです。簡単な例を次に示します。
describe('replacement', function(){
var text;
beforeEach(function(){
text = 'Some message with a newline \n or carriage return \r';
text.replace(/(?:\\[rn])+/g, ' ');
text.replace(/\s\s+/g, ' ');
});
it('should replace instances of \n and \r with spaces', function(){
expect(text).toEqual('Some message with a newline or carriage return ');
});
});
これは成功します。このシナリオを考えると、複数のスペースを単一のスペースに削減するために、置換もフォローアップします。また、この場合、ステートメントbeforeEach
内で期待する前に割り当てと関数への呼び出しを使用できるため、 は必要ありません。のように裏返して読むit
と、操作と同様に機能するはずです。string.match
expect(string.match(/someRegEx/).toBeGreaterThan(0);
お役に立てれば。
-C§
編集: または、str.replace(/regex/);
orstr.match(/regex/);
を呼び出される関数に圧縮して、spyOn
そこに使用spyOn(class, 'function').and.callthrough();
して使用し、 (単に関数を呼び出すのではなく) andbeforeEach
のようなものを使用すると、replace orの戻り値をテストできます。試合のために。expect(class.function).toHaveBeenCalled();
var result = class.function(someString);
expect(class.function(someString)).toEqual(modifiedString);
expect(class.function(someString)).toBeGreaterThan(0);
これによりさらに深い洞察が得られた場合は、気軽に +1 してください。
ありがとう、
C§