简体   繁体   English

你们能帮我在 jasmine 中为下面这段代码编写测试用例吗

[英]Can you guys help me to write the test cases in jasmine for this below piece of code

Here I've two arrays and i need to insert the objects in array from another array.这里我有两个 arrays ,我需要从另一个数组中插入数组中的对象。 Below is my code下面是我的代码

scope.newBal=[];
scope.actualBal=[
{date:2020-02-04,
bal:100.0},
{date:2020-02-09,
bal:530.0},
{date:2020-02-16,
bal:190.0},
{date:2020-02-23,
bal:4560.0}];

scope.newBal=scope.actualBal.filter(b => b.date.isAfter(startDate));

In the above code I had stored the objects in *newBal* array, after the startDate from *actualBal* array. 
So in my *newBal* array I've the below data:-
scope.newBal=[{date:2020-02-16, bal:190.0}, {date:2020-02-23, bal:4560.0}];

But i'm unable to write the test cases of above code in jasmine, So can you guys help me in this.


To write unit test cases in jasmine for above piece of code first of all initialise associated variables with dummy data在 jasmine 中为上述代码编写单元测试用例,首先使用虚拟数据初始化相关变量

  • startDate with any particular date. startDate与任何特定日期。
  • scope.newBal as empty array scope.newBal作为空数组
  • scope.actualBal array with two objects, one having date prior to startDate and another post startDate scope.actualBal数组有两个对象,一个具有在 startDate 之前的日期,另一个在 startDate 之后

Then call the function which is encapsulating above piece of code.然后调用封装上述代码的 function。 After this you can make following assertions在此之后,您可以做出以下断言

  • scope.newBal array has one element which is object with date post startDate . scope.newBal数组有一个元素,即 object ,日期在startDate之后。
  • scope.actualBal has not been changed and is same as initialised before. scope.actualBal没有改变,和之前初始化的一样。

Here is the code snippet implementing above steps -这是实现上述步骤的代码片段 -

it('scope.myFunction() should copy all objects from scope.actualBal post startDate to scope.newBal', function() {
  // setup - initialise scope variables
  scope.startDate = new Date(2020, 03, 26); // year, 0-indexed month, date;
  scope.newBal = [];
  var balance_prior_date = {date:new Date(2020, 03, 25), bal:100.0};
  var balance_post_date = {date:new Date(2020, 03, 27), bal: 50.0};
  scope.actualBal = [balance_prior_date, balance_post_date];

  // action - call encapsulating function
  scope.myFunction();

  // assert
  // only relevant object shall be copied to newBal array
  expect(scope.newBal.length).toBe(1);
  expect(scope.newBal).toContain(balance_post_date);
  // actualBal array should remain as it is
  expect(scope.actualBal.length).toBe(2);
})

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

相关问题 你们能帮我订购dom吗? - can you guys help me with the order for the dom? 请建议如何为以下angularjs代码编写一个Jasmine测试用例 - Please suggest how to write a Jasmine Test case for below angularjs code 我的图像没有出现在画布上有问题,你们可以帮我吗? - i have a problem with my image not appearing on my canvas can you guys help me? 谁能帮助我注意这一小段代码 - who can help me eyeball this small piece of code 有人可以帮助我理解这段代码中发生的事情吗 - Can somebody help me in understanding what is happening in this piece of code 有人可以帮我理解这段 Javascript 代码吗? - Can someone help me understand this piece of code in Javascript? 您能帮我用Sass代码解释`&`吗? - Can you help me Interpreting `&` in Sass code? 我在我的小项目 Calculator 上执行 JavaScript 实现时遇到了一些错误。 伙计们,你能帮帮我吗? - I am encountering some errors while doing the JavaScript implementation on my little project, Calculator. Can you help me, guys? 如何为以下 angular 方法编写测试用例 - How to write Test cases for below angular method 您可以使用Jasmine测试HTML页面中嵌入的JavaScript / JQuery代码吗? - Can you test JavaScript / JQuery code embedded in HTML page with Jasmine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM