简体   繁体   English

如何在茉莉花中测试功能表达

[英]How to test function expression in jasmine

I am learning jasmine. 我正在学习茉莉花。 Trying to test the following code. 尝试测试以下代码。

Not sure how can i create a personModule object in jasmine and how to use and test it . 不知道如何在茉莉花中创建一个personModule对象,以及如何使用和测试它。 Any help would be great. 任何帮助都会很棒。

 window.onload = function () { var module = personModule; module.init(); } var personModule = (function (module) { module.getName = function () { alert("my name" + "is Xyz"); } module.init = function () { alert("in init"); } return module; })(personModule||{}); 

Can someone please help me on this 有人可以帮我吗

Thanks 谢谢

I dont think there is anything specific to jasmine in your code. 我认为您的代码中没有茉莉特有的内容。 Its a regular function in JS. 它是JS中的常规功能。

Also, your methods doesn't have anything to test as you only have alerts inside the functions. 同样,您的方法没有任何要测试的内容,因为您在函数内部只有警报。

You probably want to test if your getName returns something. 您可能想要测试您的getName是否返回某些内容。 like 喜欢

getName: function(){
  return "some name";
}

then you could assert and test. 那么您可以断言和测试。

assert(getName()).toBe("some name");

In your code I dont see a need for you to use IIFE with personModule || 在您的代码中,我认为您不需要将IIFE与personModule ||一起使用 {} {}

You could refactor your code like below to make it a testable module. 您可以像下面那样重构代码,使其成为可测试的模块。

 window.onload = function() { personModule.init(); } var personModule = (function() { var module = {}; module.getName = function() { return "my name is Xyz"; } module.init = function() { alert("in init"); return true; } return module; })(); 

You could have your jasmine tests as below. 您可以进行如下茉莉花测试。

describe("Test Person Module", function() {
  it("Name to return default value", function() {
    expect(personModule.getName()).toBe("my name is Xyz");
  });

  it("Init to be Succesful", function() {
    expect(personModule.init()).toBe(true);
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM