using Jasmines spyon upon a private method

前端 未结 9 1606
轻奢々
轻奢々 2021-01-31 01:12

is it possible to use Jasmine unit testing framework\'s spyon method upon a classes private methods?

The documentation gives this example but can this be flexivble for a

9条回答
  •  走了就别回头了
    2021-01-31 01:52

    if you use Typescript for your objects, the function isn't really private.
    All you need is to save the value that returned from spyOn call and then query it's calls property.

    At the end this code should work fine for you (at least it worked for me):

    describe("Person", function() {
        it("calls the sayHello() function", function() {
            var fakePerson = new Person();
            // save the return value:
            var spiedFunction = spyOn(fakePerson, "sayHello");
            fakePerson.helloSomeone("world");
            // query the calls property:
            expect(spiedFunction.calls.any()).toBeFalsy();
        });
    });
    

提交回复
热议问题