在茉莉2.X它可以檢查一個函數被調用這樣的次數(當然這是假設你刺探說功能):
expect(myservice.someFunction.calls.count()).toBe(someNumber);
在TS中編寫Jasmine測試與JS非常相似。就我所經歷的唯一真正的區別是語法。你現在可以爲你的變量輸入強類型,而不是使用它來描述你的變量,並且用你將使用胖箭頭語法的函數描述塊。因此,而不是
describe('Test', function() {
it('true is true', function() {
expect(true).toBe(true);
})
})
你會寫你的測試這樣
describe('Test',() => {
it('true is true',() => {
expect(true).toBe(true);
})
})
這裏是測試我已經寫在TS小做作樣本。
namespace UserList.Services.Tests {
describe("UserList: GetEntityTypeService",() => {
let service: UserList.Services.GetEntityTypeService;
beforeEach(() => {
angular.mock.module("App.UserList.Module");
angular.mock.module(($provide: angular.auto.IProvideService) => {
$provide.service("UserList.Services.GetEntityTypeService", UserList.Services.GetEntityTypeService);
});
inject(($injector: angular.auto.IInjectorService) => {
service = <UserList.Services.GeteEntityTypeService>($injector.get("UserList.Services.GetEntityTypeService"));
});
});
it("should be defined",() => {
expect(service).toBeDefined();
});
});
}
記住TS是JS的超集。所以任何有效的JS都是有效的TS。如果你真的願意,你可以用普通的JS寫你的測試,它仍然可以工作。
至於你關於分離問題的問題,我認爲你是在正確的軌道上。這是我遵循的模式,效果很好。
- 描述具有類型和名稱的對象。
- 加載對象的模塊。
- 根據需要加載模擬模塊。
- 注入依賴和間諜的方法。
- 初始化對象:
- 服務只需要注入。
- 控制器使用$控制器服務實例化。
- 我們需要$編譯指令。
- 編寫在describe塊中分組的預期值。
在必要時測試對服務,事件和預期結果的狀態,同步和異步調用。
很好的答案謝謝。 – SwimmingG