单元测试茉莉花中的角度服务方法

时间:2015-09-23 08:31:18

标签: javascript angularjs unit-testing jasmine karma-jasmine

我有一个简单的角度服务,它调用方法(this.updateVariable),解析收到的值,并用字符串更新变量(myString)。

我想了解如何编写单元测试来测试接收到的值是否正在使用正确的字符串更新变量。

服务:

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;
    };
});

1 个答案:

答案 0 :(得分:4)

您对服务的测试可能如下所示:

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.");
    });
  });

});