admin管理员组

文章数量:1323529

I have a simple question. How do I unit test a function which is dependent on a parameter? Like say for example:

Code:

function a(param) {
   if(param > 0) 
      return param+value;
   else
      return param;
}

How do I unit test function a without having param? I hear this I use mocks or spies in jasmine. Can someone show me an example, I am really confused. Thank you all in advance.

Edit:

Thank you for such a conprehensive answer David. I really appreciate it. Here is more information about my question.

This is in fact my true question, I have a file

snap-fed.js :

//Code here...

Which I would like to unit test prehensively as you showed me. But I am unsure as to how to do this using jasmine or mocha.

Like how could I test any of the methods of the snap object? How could I unit test snap.eligibility or snap.isSnapResourceEligibile? I have been stuck on this issue for about 2 days, I really don't understand.

They all take in a parameter info which provides info about the object being worked on by the methods.

This was my true question but I did not know how to ask it.

Edit 2:

Based on David's template I made this, but it doesn't even run...

snap-fed.spec.js :

describe("snap-fed", function() { 

  describe("Properties", function() { 

    it("should exist", function() {
      expect(Allowance).not.toBeUndefined(); 
      expect(AllowanceAdditional).not.toBeUndefined();
      expect(MaxAllowanceHouseholdSize).not.toBeUndefined();
    }); 

    it("should contain correct values", function() {
        expect(Allowance).toEqual([189, 347, 497, 632, 750, 900, 995, 1137]);
        expect(AllowanceAdditional).toBe(142);
        expect(MaxAllowanceHouseholdSize).toBe(Allowance.length);
    }); 
  });

  describe("Functions", functions(){

    it("should return the expected result", function() {
      expect(snap.isSnapResourceEligible(info)).toBeTruthy();
    });

    //Put more test cases for the various methods of snap

  });

}); 

I have a simple question. How do I unit test a function which is dependent on a parameter? Like say for example:

Code:

function a(param) {
   if(param > 0) 
      return param+value;
   else
      return param;
}

How do I unit test function a without having param? I hear this I use mocks or spies in jasmine. Can someone show me an example, I am really confused. Thank you all in advance.

Edit:

Thank you for such a conprehensive answer David. I really appreciate it. Here is more information about my question.

This is in fact my true question, I have a file

snap-fed.js :

//Code here...

Which I would like to unit test prehensively as you showed me. But I am unsure as to how to do this using jasmine or mocha.

Like how could I test any of the methods of the snap object? How could I unit test snap.eligibility or snap.isSnapResourceEligibile? I have been stuck on this issue for about 2 days, I really don't understand.

They all take in a parameter info which provides info about the object being worked on by the methods.

This was my true question but I did not know how to ask it.

Edit 2:

Based on David's template I made this, but it doesn't even run...

snap-fed.spec.js :

describe("snap-fed", function() { 

  describe("Properties", function() { 

    it("should exist", function() {
      expect(Allowance).not.toBeUndefined(); 
      expect(AllowanceAdditional).not.toBeUndefined();
      expect(MaxAllowanceHouseholdSize).not.toBeUndefined();
    }); 

    it("should contain correct values", function() {
        expect(Allowance).toEqual([189, 347, 497, 632, 750, 900, 995, 1137]);
        expect(AllowanceAdditional).toBe(142);
        expect(MaxAllowanceHouseholdSize).toBe(Allowance.length);
    }); 
  });

  describe("Functions", functions(){

    it("should return the expected result", function() {
      expect(snap.isSnapResourceEligible(info)).toBeTruthy();
    });

    //Put more test cases for the various methods of snap

  });

}); 
Share Improve this question edited Sep 3, 2014 at 20:41 mosawi asked Sep 2, 2014 at 6:41 mosawimosawi 1,3235 gold badges27 silver badges49 bronze badges 2
  • 1 You provide param in as an argument in the unit test itself, then assert that the return value is as expected. – adairdavid Commented Sep 2, 2014 at 6:43
  • Can you please give me an example? What you're saying makes sense, but I think if I saw an example, I would understand much better – mosawi Commented Sep 2, 2014 at 6:56
Add a ment  | 

1 Answer 1

Reset to default 4

The test could look like this in Jasmine:

    describe("Function a", function() {

      it("is defined", function() {
        expect(a).not.toBeUndefined();
      });

      it("should return expected result for a positive parameter", function() {
        var result = a(19);

        expect(result).toEqual(24);
      });

      it("should return expected result for a negative parameter", function() {
        var result = a(-1);

        expect(result).toEqual(-1);
      });

      it("should return expected result for parameter zero", function() {
        var result = a(0);

        expect(result).toEqual(5);
      });

    });

This scenario is expecting the variable value inside function a to be a constant equal to 5. The scenario can be more plex and the tests would look differently. If there is any logic about the value variable, please show it in your question, I will then edit my answer.

EDIT: test sample for the second part of your question

So if the eligibility function looked like this after removing the Q.fcall:

eligibility: function (info) {
            return {
                eligible : snap.isSnapIneEligible(info) && snap.isSnapResourceEligible(info),
                savings : snap.calcSavings(info)
            };
    }

Then you could test the snap object like this for example:

describe("Snap", function() {

  var snap = ... //get the snap object here...

  var disabledMemberInfo = {
    disabledMember: true,
    age: 50,
    fpl: 1.2,
    householdSize: 10,
    moneyBalance: 4000
  };

  it("is defined", function() {
    expect(snap).not.toBeUndefined();
  });

  it("has eligibility defined", function() {
    expect(snap.eligibility).not.toBeUndefined();
  });

  it("return positive eligibility for disabled member with fpl < 1.2 and money balance 4000", function() {

    var result = snap.eligibility(disabledMemberInfo);

    expect(result).not.toBeUndefined();
    expect(result.eligible).not.toBeUndefined();
    expect(result.savings).not.toBeUndefined();

    expect(result.eligible).toEqual(true);
  });

});

I didn't cover the whole snap object with tests. But more test will be similar, my code should be an example and more tests built in a similar way.

本文标签: How to unit test javascript function which takes in a parameter using jasmine or mochaStack Overflow