Jasmine object test state when calling an Ajax spy

I am testing a module with an Angular controller that uses the Rails Resource factory to process GETing and POSTing model data from and to the Rails application. POSTing is done using a method on the model, for example. (with model $scope.resource):

$scope.resource.update().then(successHandler, failureHandler);

I have a spy on this method to drown out Ajax calls, so I can unit test the controller:

resUpdateSpy = spyOn($scope.resource, 'update').and.callFake(function() { 
  return {then: function(success, failure){ success(resUpdateResponse); }};
});

In one of my controller methods, I expect the resource to be POSTED with certain data (in particular, Stripe data). The data will be overridden after POST in the same method, so I can’t check the state of the model after that. Ideally, I would like something like:

expect($scope.resource.update).toHaveBeenCalled().whileValueOf($scope.resource.stripeKey).isEqualTo('tok123');

Obviously, this method does not exist in vanilla jasmine. Is there a way in Jasmine (or vanilla or through a third-party project) to check the status of the value when you call this spy? or is there another way to check this situation - in particular, the state of the model before its POST messages - what am I missing?

I am running Jasmine 2.2.0 with Teaspoon 1.0.2 in an Angular 1.3.14 application.

+4
source share
2 answers

Since you already have your spy on calling a fake function, this fake function will save the value you are interested in:

var scopeResourceStripeKeyDuringSpyCall = '(spy not called, yet)';
resUpdateSpy = spyOn($scope.resource, 'update').and.callFake(function() {
  scopeResourceStripeKeyDuringSpyCall = $scope.resource.stripeKey;
  return {then: function(success, failure){ success(resUpdateResponse); }};
});

Then just check the stored value in your statement:

expect(scopeResourceStripeKeyDuringSpyCall).toEqual('tok123');

(Depending on whether the spy settings and approval are in the same area, the variable to save the value may be less local.)

+1

jasime, , ,

jasmine.Matchers.prototype.toBeResolvedWith = function() {
  var done, expectedArgs;
  expectedArgs = jasmine.util.argsToArray(arguments);
  if (!this.actual.done) {
    throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
  }
  done = jasmine.createSpy('done');
  this.actual.done(done);
  this.message = function() {
    if (done.callCount === 0) {
      return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.", "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."];
    } else {
      return ["Expected spy " + done.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall), "Expected spy " + done.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(done.argsForCall)];
    }
  };
  return this.env.contains_(done.argsForCall, expectedArgs);
};

https://gist.github.com/gr2m/2191748

:

. - toBe, toEqual .. , .

var customMatchers = {

toHaveBeenResolved: function(util, customEqualityTesters) {
  return {
    compare: function(actual, expected) {
      var result = {};
      // do you comparison logic here
      result.pass = true/false;
      result.message = 'some message about test result';
      return result;
    }
}

this.actual - ,

result = {};
promise.then(function(value){
  result.value = value;
  result.status = 'Resolved';
}, function(value){
  result.value = value;
  result.status = 'Rejected';
});

, , .

beforeEach(function() {
  jasmine.addMatchers(customMatchers);
}); 
+3

Source: https://habr.com/ru/post/1599553/


All Articles