Вопрос

Here is my Test

describe("setTimer", function () {
      it("set status timer values from parameters and sets timer.visible to true", function(){
        var boxNumber = 1,
          time = 15;
        myObject.setTimer(boxNumber, time);
        expect(anotherObject.status.timer.boxNum).toBe(boxNumber);
        expect(anotherObject.status.timer.seconds).toBe(time);
      })
  });

Here is the code

 setTimer: function (boxNum, seconds) {
    anotherObject.status.timer.boxNum = boxNum;
    anotherObject.status.timer.seconds = seconds;
    anotherObject.status.timer.visible = true;
  },

Here is the error I am getting

TypeError: Cannot read property 'timer' of undefined

I tried setting the object using anotherObject = {} I tried setting anotherObject.status = {} and lastly tried setting anotherObject.status.timer = {}, however I still get the error. Any ideas, how can I mock the object?

Это было полезно?

Решение

Without knowing how/where 'anotherObject' is constructed I would think that you would need to initialize the 'anotherObject' before you execute the setTimer function in your test.

Do you have an init() or setup() function that exists on 'anotherObject' that would initialize the 'timer' object for you?

Although the method looks like it is just trying to make sure that the method is setting all the corresponding properties.

You could do the following before calling setTimer in your test

describe("setTimer", function () {
  it("set status timer values from parameters and sets timer.visible to true", function(){
    var boxNumber = 1,
      time = 15;

    //Initialize the anotherObject
    anotherObject.status = { timer : {} }

    myObject.setTimer(boxNumber, time);
    expect(anotherObject.status.timer.boxNum).toBe(boxNumber);
    expect(anotherObject.status.timer.seconds).toBe(time);
  })
});

This of course comes with the caveat that you have now defined an 'anotherObject' inside your test using the global scope (since excluding the var on any variable definition in javascript makes it global scope). This could effect other test cases that expect the timer object to be setup a certain way but your test case has now set the timer values to 1 and 15 respectively (could be alot of other values all depending on what the test case is doing).

So to help with this, resetting the 'anotherObject' at the beginning or end of your tests would help with pollution

afterEach(function(){ 
    anotherObject.status = { timer : {} }
})

or

beforeEach(function(){
    anotherObject.status = { timer : {} }
})

Of course if you have an init(), create() or setup() function on the 'anotherObject' that could be used it would of course give you more realistic results since the object would be much closer to what it would look like in production.

Другие советы

You are not working on the same "anotherObject" object in both source and test codes.

Each code has it's own object and setting values to one will not set in the other.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top