【问题标题】:Jasmine to test whether a object has a certain method or notJasmine 测试一个对象是否有某种方法
【发布时间】:2013-03-18 13:40:58
【问题描述】:

我正在使用Jasmine,我想测试一个对象是否有某种方法,如下所示:

it "should have 'open' method", ->
    @xhr = ajax.create()
    expect(@xhr).toHaveMethod "open" # I want to test like this!

我该如何测试?谢谢你的好意。

【问题讨论】:

    标签: javascript coffeescript jasmine


    【解决方案1】:

    我会尝试:

    it "should have 'open' method", ->
        @xhr = ajax.create()
        expect(@xhr.open).toBeDefined()
    

    看到这个fiddle

    【讨论】:

      【解决方案2】:

      没有内置的方法可以做到这一点,但您可以通过这样做来达到预期的效果。

      it "should have 'open' method", ->
          @xhr = ajax.create()
          expect(typeof @xhr.open).toBe("function")
      

      请注意,测试是否按照其他答案中的建议进行了定义,在某些极端情况下它会通过,但不应该通过。如果你采取以下结构,它就会通过,它肯定不是一个函数。

      { open : 1 }
      

      【讨论】:

      • 这个解决方案的问题是,如果期望失败,消息将类似于Expected undefined to be function,这并不是那么有用。像Expected object '@xhr' to have property 'open' 这样的东西会更好。
      【解决方案3】:

      @david 回答正确。 toBeDefined() 可能是您想要的。然而,如果你想验证它是一个函数而不是一个属性,那么你可以使用toEqual()jasmine.any(Function)。这是一个例子:http://jsbin.com/eREYACe/1/edit

      【讨论】:

        【解决方案4】:

        我尝试了这个解决方案并且它有效:

        spyOn(@xhr,"open")
        

        如果没有打开函数,它会抛出错误,因为它无法在上面启动 spyig

        【讨论】:

          【解决方案5】:

          Jasmine 允许您编写自己的“匹配器”。文档对此进行了解释。 http://jasmine.github.io/2.0/custom_matcher.html

          您可以编写一个非常具体的匹配器,称为

          expect(obj).toHaveMethod("methodName");
          

          我个人会写一些更通用的东西来检查值类型。这样,我不仅可以使用它来检查是否在对象/实例上定义了方法,还可以检查任何可以存储值的东西。它还让我可以检查函数类型以外的类型。

          expect(obj.methodName).toBeA(Function);
          

          要让它工作,你必须确保添加 toBeA“匹配器”。

          beforeEach(function(){
              jasmine.addMatchers({
                toBeA: toBeA
              });
          });
          
          
          function toBeA() {
            return {
              compare: function (value, type) {
                var result = {pass: val != null && val.constructor === type || val instanceof type};
          
                if (result.pass) {
                  result.message = 'Expected ' + value + ' to be a ' + type.name
                } else {
                  result.message = 'Expected ' + value + ' to not be a ' + type.name
                }
          
                return result;
              }
            };
          }
          

          【讨论】:

            【解决方案6】:

            我是这样做的。角度示例:

            beforeEach(inject(function ($injector) {
                service = $injector.get("myService");       
            }));
            
            it("should have myfunction function", function () {
                expect(angular.isFunction(service.myfunction)).toBe(true);
            });
            

            【讨论】:

            • 什么是“角”?
            猜你喜欢
            • 2011-07-03
            • 2010-10-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多