Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a method that does not return any value(jasmine angular4)

I'm working on a project where i'm using jasmine for testing. I have a situation where a method does not return any value but it just sets the class property. I know how to use spy on a method that returns a value but not sure how to use it on a method that does not return any value.I searched online but couldn't find any proper resource. And the method is as follows

updateDvStatus() {
    this._http.get(this.functionControlUrl).subscribe(() => {
    this._active = true;
    }, (error) => {
      this.errorStatusCode = error.status;
      this.errorPageService.setStatusCode(this.errorStatusCode);
    })
  }

Any help would be appreciated.

like image 224
Ankitha Muralidhar Avatar asked Oct 04 '17 09:10

Ankitha Muralidhar


1 Answers

How to test a method that does not return anything

The following is an example of testing a method that doesn't return anything.

var serviceUnderTest = {
  method: function() {
    console.log('this function doesn't return anything');
  }
};

it('should be called once', function() {
  spyOn(serviceUnderTest, 'method');

  serviceUnderTest.method();

  expect(serviceUnderTest.method.calls.count()).toBe(1);
  expect(serviceUnderTest.method).toHaveBeenCalledWith();
});

How to test a callback

I suspect your real problem though is testing that the function you are passing into the subscribe() function does what you expect. If that is what you are really asking, then the following might be helpful (note that I wrote this off the top of my head, so there might be a typo).

var serviceUnderTest = {
  method: function() {
    this.someOtherMethod(function() { this.active = true; });
  },
  someOtherMethod: function(func) {
    func();
  }
}

it('should execute the callback, setting "active" to true', function() {
  spyOn(serviceUnderTest, 'someOtherMethod');

  serviceUnderTest.method();

  expect(serviceUnderTest.someOtherMethod.calls.count()).toBe(1);
  var args = serviceUnderTest.someOtherMethod.calls.argsFor(0);
  expect(args.length).toBeGreaterThan(0);
  var callback = args[0];
  expect(typeof callback).toBe('function');

  expect(serviceUnderTest.active).toBeUndefined();
  callback();
  expect(serviceUnderTest.active).toBe(true);
});

Your Scenario

Sorry for the older syntax, I'm writing this from my head, so I'd rather it work, than be cool looking, but have some typos. Also, I haven't used Observables, so there is likely a better way to test them than what I am about to show you, and it probably amounts to creating a new Observable, and spying on subscribe. Since this is off the top of my head, we will have to make do.

it('should subscribe with a function that sets _active to true', function() {
  // Arrange
  var observable = jasmine.createSpyObj('Observable', ['subscribe']);
  spyOn(http, 'get').and.returnValue(observable);

  // Act... (execute your function under test)
  service.updateDvStatus();

  // Assert
  expect(http.get.calls.count()).toBe(1);
  expect(http.get).toHaveBeenCalledWith(service.functionControlUrl);
  expect(observable.subscribe.calls.count()).toBe(1);
  var args = observable.subscribe.calls.argsFor(0);
  expect(args.length).toBeGreaterThan(0);
  var callback = args[0];
  expect(typeof callback).toBe('function');

  service._active = false;
  callback();
  expect(service._active).toBe(true);
});
like image 169
Seth Flowers Avatar answered Oct 26 '22 23:10

Seth Flowers