[英]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.