javascript - 全局对象内 JavaScript 参数的奇怪行为

标签 javascript jasmine

全局 Javascript 对象内的参数有一个奇怪的行为: 我需要使用 jasmine.js 测试我的代码,但我无法将预期值传递给参数,在 jasmine 测试中始终返回未定义。

//My model
myGlobalObject = function(){
  _myCart = function(){
      return {
        total : 0,
        products : []
       }
   }

   return {
      init: function(strangeArgument){
        console.log(strangeArgument) //this return undefined in jasmine test
       },

     myCart : _myCart,

     addProduct : function(Products){
       return _myCart()
     },

    .....
   }

}

测试:

const c{
   empty : {
      total: {
        beforeVAT: 0,
        afterVAT: 0,
        VAT: 0
      },
     products: []
  }
}

beforeEach(() => {
    this.instance = myGlobalObject();
    this.instance.init();
    this.productWithoutQuantity = Object.assign({}, _.productA);
    delete this.productWithoutQuantity.quantity;
    this.productWithQuantity = Object.assign({}, _.productB);
  });

test(`the cart should be empty`, () => {
    expect(this.instance.getCart()).toEqual(c.empty);
  });

.... more tests

还有我的主要js:

var e = myGlobalObject();
var initialState = function (){
   return {
      total: {
        beforeVAT: 0,
        afterVAT: 0,
        VAT: 0
      },
     products: []
  }
}
e.init(initialState);

哪里出了问题?

最佳答案

虽然我无法完全理解OP的意图,但以下是我对这个问题的看法

  • _myCart 可以是局部变量,因为它似乎没有任何更大的用途,至少从 OP 提供的代码来看

  • instance.init 的调用可以使用空括号或合法变量 - 取决于 OP 在这里试图实现的目标。

  • 我已经包含了 main.js 代码片段以及 testVariable.instance.init(); (简单来说,如果它是未定义的,则它是未定义的,正如 @ 所评论的)贝尔吉)

  • 查看实际效果 here

myGlobalObject = function() {
  this._myCart = function() {
    return {
      total: 0,
      products: []
    }
  }

  return {
    init: function(strangeArgument) {
      console.log(strangeArgument)
    },
    myCart: this._myCart,
    addProduct: function(Products) {
      return this._myCart()
    }
  }

}

var e = myGlobalObject();
var initialState = function() {
  return {
    total: {
      beforeVAT: 0,
      afterVAT: 0,
      VAT: 0
    },
    products: []
  }
}
e.init(initialState);

describe('ajax test suite', function() {
  var testVariable = {}
  var c = {
    empty: {
      total: 0,
      products: []
    }
  }

  beforeEach(function() {
    testVariable.instance = myGlobalObject();
    testVariable.instance.init("hello");
    testVariable.instance.init();
  });

  it('the cart should be empty', function() {
    expect(testVariable.instance.myCart()).toEqual(c.empty);
  });
});

关于javascript - 全局对象内 JavaScript 参数的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44456565/

相关文章:

javascript - react 代码中的@internal JavaScript 文档标记,这是 jsdoc、闭包还是其他?

javascript - 错误: inject Angular $http service into requires plugin

Angular2 构建失败,出现 "Cannot find name ' 文档'”;其他引用也无法解析

javascript - 在meteor-jasmine中测试router.go的点击事件

javascript - 如何在 knockout.js foreach 绑定(bind)中有条件地呈现 tr

javascript - 来自本地文件的 Ajax 请求

javascript - javascript中的拖放功能

javascript - "spec"在 Javascript 测试中意味着什么

angular - 为什么我的测试不使用服务 stub 方法?

AngularJS promise 在使用 Jasmine 进行测试时不会调用 then