jeudi 23 juillet 2020

How to assert Javascript object key value pairs using Jasmine

In A.js, I have:

class Cla{
 let g_up = {}
 function func(key){
    g_up[key] = check_value(key);  // check_value returns a boolean
    return;
 }
}

and now I want to mock return value of check_value function and assert g_up contains correct information. In A.t.js:

describe("test", function() {
  it('test false', function () {

        check = jasmine.createSpy("check_value");
        Cla.check_value = check;
        check.and.callFake(function(key){
            if (key == "key1"){
                return false;
            }
        });    // mock return value of check_value function
       spyOnProperty(Cla.g_up, "key1", 'get'); 
       Cla.func("key1");
       expect(Cla.g_up["key1"]).toEqual(false);  // check map should be like g_up = {"key1": false}
  });
}

However, I got errors: Error: spyOn could not find an object to spy upon for key1

How should I fix it and use Jasmine to get and assert values of g_up?

Aucun commentaire:

Enregistrer un commentaire