jeudi 23 juillet 2020

How can I expect a global variable map using Jasmine

I am testing function A in A.js:

class C{
    let g_map = {};
    function A(key, value){
       g_map[key] = api(key);
       if (g_map[key]){
         return true;
       }
       
      let res = g_cfg.is_key_editable(key, value);
      if (!res.is_editable){
        return false;
      }
      ...
      ...
      return true;
    }
}

In my test, I mock the return value of api and tried to make sure it works fine. In my A.t.js:

it('api true', function(){
    const ap = jasmine.createSpy("api");
    C.api = ap;
    ap.and.callFake(function(key){
        if (key == 1){
           return true;
        }
    })
    expect(C.A(1)).toEqual(true);
});

But I also want to assert values of g_map, because if I can assert values in g_map, I can also test false, now even if I mock api to return false, there is still bunch of logic before end of this function which I do not care, so I tried:

    it('test true', function(){
        spyOn(C, "g_map");
       const ap = jasmine.createSpy("api");
        C.api = ap;
        ap.and.callFake(function(key){
            if (key == 1){
               return true;
            }
        })
        expect(C.A(1)).toEqual(true);
        expect(g_map[1]).toEqual(true);
 });

but I got errors <spyOn> g_map() method does not exist

So how should I get the value of g_map and assert it? I am new to Jasmine, thanks!

Aucun commentaire:

Enregistrer un commentaire