問題描述
我有兩個對象被 Jasmine 設置為間諜:
I've got two objects that have been set up as spies with Jasmine:
spyOn(obj, 'spy1');
spyOn(obj, 'spy2');
我需要驗證對 spy1
的調用是否在對 spy2
的調用之前.我可以檢查它們是否都被調用:
I need to verify that calls to spy1
come before calls to spy2
. I can check if both of them are called:
expect(obj.spy1).toHaveBeenCalled();
expect(obj.spy2).toHaveBeenCalled();
但即使首先調用 obj.spy2()
也會通過.有沒有一種簡單的方法可以驗證一個在另一個之前被調用?
but this will pass even if obj.spy2()
was called first. Is there an easy way of verifying that one was called before the other?
推薦答案
貌似 Jasmine 的人看到這個帖子或者其他人喜歡,因為 這個功能存在.我不確定它已經存在了多久——他們所有回到 2.6 的 API 文檔都提到了它,盡管他們存檔的舊式文檔都沒有提到它.
Looks like the Jasmine folks saw this post or others like it, because this functionality exists. I'm not sure how long it's been around -- all of their API docs back to 2.6 mention it, though none of their archived older style docs mention it.
toHaveBeenCalledBefore(預期
)
expect 實際值(a Spy) 在另一個 間諜.
toHaveBeenCalledBefore(
expected
)
expect the actual value (a Spy) to have been called before another Spy.
參數:
Name Type Description
expected Spy Spy that should have been called after the actual Spy.
您的示例失敗看起來像 Expected spy spy1 to have been called before spy spy2
.
A failure for your example looks like Expected spy spy1 to have been called before spy spy2
.
這篇關于有沒有辦法用 Jasmine 驗證間諜執行的順序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!