简体   繁体   中英

Unit testing an angular service method in jasmine

I have a simple angular service which calls a method ( this.updateVariable ), parses the value received, and updates a variable ( myString ) with a string.

I want to understand how can i write a unit test to test if the value received is updating a variable with the correct string.

Service:

app.service('myService', function() {

    this.updateVariable = function (status, data) {

        var myString = '';

        if (status.toString()[0] == 4 && data.message !== undefined) {

            if ((data.message.indexOf("Out of spoons") > -1) || (data.message.indexOf("Out of forks")) > -1){
                myString = "Sorry, weve ran out of spoons and/or forks";
            }
            else if (data.message.indexOf("Out of plates") > -1) {
                myString = "Sorry, weve ran out of plates";
            }
            else {
                myString = "We seem to be facing a technical issue";
            }
        }

        if (status.toString()[0] == 9) {
            myString = "Ooops, looks like something is broke.";
        }

        return myString;
    };
});

Your test for your service could look something like this:

describe('myService Tests', function () {
  beforeEach(module('myApp'));

  beforeEach(inject(function (_myService_) {
    this.sut = _myService_;
  }));

  describe('when status 400', function () {
    beforeEach(function () {
      this.status = 400;
    });

    describe('and no message', function () {
      it('should return empty string', function () {
        //arrange
        var data = {};

        //act
        var result = this.sut.updateVariable(this.status, data);

        //assert
        expect(result).toBe("");
      });
    });

    describe('and out of spoons message', function () {
      it('should return out of spoons/forks string', function () {
        //arrange
        var data = {
          message: "Out of spoons"
        };

        //act
        var result = this.sut.updateVariable(this.status, data);

        //assert
        expect(result).toBe("Sorry, weve ran out of spoons and/or forks");
      });
    });

    describe('and out of plates message', function () {
      it('should return out of plates string', function () {
        //arrange
        var data = {
          message: "Out of plates"
        };

        //act
        var result = this.sut.updateVariable(this.status, data);

        //assert
        expect(result).toBe("Sorry, weve ran out of plates");
      });
    });

    describe('and no spoons or plates message', function () {
      it('should return technical issue string', function () {
        //arrange
        var data = {
          message: "Some other message"
        };

        //act
        var result = this.sut.updateVariable(this.status, data);

        //assert
        expect(result).toBe("We seem to be facing a technical issue");
      });
    });
  });

  describe('when status 900', function () {
    beforeEach(function () {
      this.status = 900;
    });

    it('should return something broke string', function () {
      //arrange
      var data = {};

      //act
      var result = this.sut.updateVariable(this.status, data);

      //assert
      expect(result).toBe("Ooops, looks like something is broke.");
    });
  });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM