【问题标题】:Jasmine: how to spy on inner object method call?Jasmine:如何监视内部对象方法调用?
【发布时间】:2013-08-31 07:03:19
【问题描述】:

我有两个要测试的原型:

var Person = function() {};

Person.prototype.pingChild = function(){
   var boy = new Child();
   boy.getAge();
}

var Child = function() {};

Child.prototype.getAge = function() {
    return 42;
};

我到底想测试什么:检查getAge() 方法是否在pingChild() 方法内部调用

这是我尝试为此目的使用的 Jasmine 规格:

describe("Person", function() {
    it("calls the getAge() function", function() {
        var fakePerson = new Person();
        var chi = new Child();
        spyOn(fakePerson, "getAge");
        fakePerson.pingChild();
        expect(chi.getAge).toHaveBeenCalled();
    });
});

describe("Person", function() {
    it("calls the getAge() function", function() {
        var fakePerson = new Person();
        spyOn(fakePerson, "getAge");
        fakePerson.pingChild();
        expect(fakePerson.getAge).toHaveBeenCalled();
    });
});

describe("Person", function() {
    it("calls the getAge() function", function() {
        var fakePerson = new Person();
        var chi = new Child();
        spyOn(chi, "getAge");
        fakePerson.pingChild();
        expect(chi.getAge).toHaveBeenCalled();
    });
});

但所有这些都只显示错误:

  • getAge() 方法不存在
  • getAge() 方法不存在
  • 预期的间谍 getAge 已被调用

那么,有没有什么方法可以使用 Jasmine 测试这种情况,如果可以,怎么做?

【问题讨论】:

    标签: javascript unit-testing testing bdd jasmine


    【解决方案1】:

    你已经窥探到了Child对象的原型。

    describe("Person", function () {
      it("calls the getAge() function", function () {
        var spy = spyOn(Child.prototype, "getAge");
        var fakePerson = new Person();
        fakePerson.pingChild();
        expect(spy).toHaveBeenCalled();
      });
    });
    

    【讨论】:

      【解决方案2】:

      我认为这是不可能的,因为内部对象无法从父对象外部访问。这完全取决于您的对象的范围。

      您可以通过这样做在Person 对象中公开您的Child 对象:

      var Person = function() {
          this.boy = new Child();
      };
      
      Person.prototype.pingChild = function(){
         this.boy.getAge();
      }
      

      然后:

      describe("Person", function() {
          it("calls the getAge() function", function() {
              var fakePerson = new Person();
              var chi = fakePerson.boy;
              spyOn(chi, "getAge");
              fakePerson.pingChild();
              expect(chi.getAge).toHaveBeenCalled();
          });
      });
      

      或者将Child的初始化委托在Person对象之外:

      var Person = function(child) {
          this.boy = child;
      };
      
      Person.prototype.pingChild = function(){
         this.boy.getAge();
      }
      

      然后:

      describe("Person", function() {
          it("calls the getAge() function", function() {
              var chi = new Child();
              var fakePerson = new Person(chi);
              spyOn(chi, "getAge");
              fakePerson.pingChild();
              expect(chi.getAge).toHaveBeenCalled();
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2021-02-04
        • 2017-08-15
        • 2018-04-20
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-16
        • 2022-09-30
        相关资源
        最近更新 更多