Inheritance 茉莉花&x2B;测试是否调用了继承的方法

Inheritance 茉莉花&x2B;测试是否调用了继承的方法,inheritance,jasmine,Inheritance,Jasmine,用Jasmine测试继承方法调用的最佳方法是什么 我只对测试它是否被调用感兴趣,因为我已经为基类设置了单元测试 例如: YUI().use('node', function (Y) { function ObjectOne () { } ObjectOne.prototype.methodOne = function () { console.log("parent method"); } function ObjectTwo

用Jasmine测试继承方法调用的最佳方法是什么

我只对测试它是否被调用感兴趣,因为我已经为基类设置了单元测试

例如:

YUI().use('node', function (Y) {


    function ObjectOne () {

    }

    ObjectOne.prototype.methodOne = function ()  {
        console.log("parent method");
    }


    function ObjectTwo () {
        ObjectTwo.superclass.constructor.apply(this, arguments);
    }

    Y.extend(ObjectTwo, ObjectOne);

    ObjectTwo.prototype.methodOne = function () {
        console.log("child method");

        ObjectTwo.superclass.methodOne.apply(this, arguments);
    }
})
我想测试ObjectTwo继承的methodOne是否被调用


提前感谢。

要做到这一点,您可以在
ObjectOne
的原型中发现该方法

spyOn(ObjectOne.prototype, "methodOne").andCallThrough();
obj.methodOne();
expect(ObjectOne.prototype.methodOne).toHaveBeenCalled();
此方法的唯一警告是,它不会检查是否对
obj
对象调用了
methodOne
。如果需要确保它是在
obj
对象上调用的,可以执行以下操作:

var obj = new ObjectTwo();
var callCount = 0;

// We add a spy to check the "this" value of the call.    //
// This is the only way to know if it was called on "obj" //
spyOn(ObjectOne.prototype, "methodOne").andCallFake(function () {
    if (this == obj)
        callCount++;

    // We call the function we are spying once we are done //
    ObjectOne.prototype.methodOne.originalValue.apply(this, arguments);
});

// This will increment the callCount. //
obj.methodOne();
expect(callCount).toBe(1);    

// This won't increment the callCount since "this" will be equal to "obj2". //
var obj2 = new ObjectTwo();
obj2.methodOne();
expect(callCount).toBe(1);

测试执行后,它不会让间谍监视ObjectOne.prototype.methodOne吗?我担心这可能会给使用此方法的其他测试带来麻烦。特别是对于.andCallFake()@challet的第二个示例,这应该不是问题。每次测试后,所有间谍都被清除。