1

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;
    };
});
2
  • 1
    Where is your test? And which variable do you want to check? I hope this is some code you are writing simply to try something out, because your function/variable naming is not very maintenance-friendly. Commented Sep 23, 2015 at 9:08
  • @codemonkey - i want to test the value of myString is correct based on the if statement. Yes, this is just very rough code as i am trying to understand testing from scratch with a simple service. Commented Sep 23, 2015 at 9:45

1 Answer 1

4

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

});
Sign up to request clarification or add additional context in comments.

2 Comments

nested describe should start with and, not when
@koen can u help me over here stackoverflow.com/questions/40072869/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.