Unit-testing ngResource $save

天涯浪子 提交于 2019-12-23 02:43:10

问题


I have a resource which wraps a RESTful API. I use that resource from my controller to create new objects and save them, similar to this snippet from the Angular docs:

var newCard = new CreditCard({number:'0123'});
newCard.name = "Mike Smith";
newCard.$save();

When writing a unit test in Jasmine, I get the following error when the $save call is executed: "Cannot read property '$promise' of undefined".

What's the best approach to testing the method in my controller which contains the above code?


回答1:


If you use Jasmine's spyOn() function to verify that a $resource method is called, it overwrites the original $resource method with one that implements the "spying" functionality.

If the code in your application relies on the $resource setting the $promise property, or it relies on the returned object/array from the $resource, the Jasmine's spy function won't return anything or set a value on the $promise property. As a result, perfectly fine code in your application will fail when being tested. A similar thing happens when you use $http with the then(), success(), or error() functions.

To work around that you can make Jasmine spy on the function as well as call the original function by doing something like this:

// Newer Jasmine 2.0 syntax:
spyOn(resource, "$save").and.callThrough();
// Older syntax:
spyOn(resource, "$save").andCallThrough();


来源:https://stackoverflow.com/questions/27049146/unit-testing-ngresource-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!