繁体   English   中英

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

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

这里我有两个 arrays ,我需要从另一个数组中插入数组中的对象。 下面是我的代码

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.


在 jasmine 中为上述代码编写单元测试用例,首先使用虚拟数据初始化相关变量

  • startDate与任何特定日期。
  • scope.newBal作为空数组
  • scope.actualBal数组有两个对象,一个具有在 startDate 之前的日期,另一个在 startDate 之后

然后调用封装上述代码的 function。 在此之后,您可以做出以下断言

  • scope.newBal数组有一个元素,即 object ,日期在startDate之后。
  • scope.actualBal没有改变,和之前初始化的一样。

这是实现上述步骤的代码片段 -

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.

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