Why would you use a spy in a test?

Technology CommunityCategory: AngularWhy would you use a spy in a test?
VietMX Staff asked 3 years ago

Jasmine has a concept called spies. You can check if it was called, what parameters it had, if it returned something or even how many times it was called.

Consider:

it('can accept a post to update it', function() {
  var postToAccept = {
    title: 'Title',
    body: 'Post'
  };
  spyOn(rest, 'update').and.callThrough();
  post.accept(postToAccept);
  expect(rest.update).toHaveBeenCalledWith(postToAccept);
});