Jasmine 抱怨 returns 承诺的函数“.then 不是函数”
Jasmine complaining that ".then is not a function" on a function that returns a promise
我正在测试一个函数,看看它是否正在调用一个 returns 承诺的函数:
函数:
saveNew: (collection, query) ->
@newId
.then (id) ->
$http.post server + "/api/v1/saveJson/#{collection}/#{id}", query
newId函数:
newId: () -> $http.get server + "/api/v1/newid"
测试:
describe "saveNew", () ->
it "should call to get a new id", () ->
spyOn(mySvc,"newId")
entitySvc.saveNew "myCollection", {data: "stuff"}
expect(entitySvc.newId).toHaveBeenCalled()
这导致
TypeError: this.newId.then is not a function
与此同时,我测试了另一种方法,该方法调用了一个 returns 承诺的函数,并且它通过了:
findOne: (collection, query) -> @find collection, query
异步函数:
find: (collection, query) ->
$http.post server + "/api/v1/findJson/#{collection}", query
测试:
describe "#findOne", () ->
it "should call #find", () ->
query = {id:1}
collection = "myCollection"
spyOn(mySvc,"find")
mySvc.findOne collection, query
expect(mySvc.find).toHaveBeenCalledWith(collection,query)
你可以通过假电话进行窥探。这允许您通过承诺 return 输出您想要的数据。
spyOn(mySvc, 'newId').andCallFake(function () {
return $.Deferred().resolve(1);
});
然后您可以验证 newId returned 是否符合您的预期
我正在测试一个函数,看看它是否正在调用一个 returns 承诺的函数:
函数:
saveNew: (collection, query) ->
@newId
.then (id) ->
$http.post server + "/api/v1/saveJson/#{collection}/#{id}", query
newId函数:
newId: () -> $http.get server + "/api/v1/newid"
测试:
describe "saveNew", () ->
it "should call to get a new id", () ->
spyOn(mySvc,"newId")
entitySvc.saveNew "myCollection", {data: "stuff"}
expect(entitySvc.newId).toHaveBeenCalled()
这导致
TypeError: this.newId.then is not a function
与此同时,我测试了另一种方法,该方法调用了一个 returns 承诺的函数,并且它通过了:
findOne: (collection, query) -> @find collection, query
异步函数:
find: (collection, query) ->
$http.post server + "/api/v1/findJson/#{collection}", query
测试:
describe "#findOne", () ->
it "should call #find", () ->
query = {id:1}
collection = "myCollection"
spyOn(mySvc,"find")
mySvc.findOne collection, query
expect(mySvc.find).toHaveBeenCalledWith(collection,query)
你可以通过假电话进行窥探。这允许您通过承诺 return 输出您想要的数据。
spyOn(mySvc, 'newId').andCallFake(function () {
return $.Deferred().resolve(1);
});
然后您可以验证 newId returned 是否符合您的预期