简体   繁体   English

单元测试以检查数组是否反转

[英]Unit test to check if array is reversed

I have the following code and I am trying to write a Jasmine unit test that will check if the array is reversed. 我有以下代码,我正在尝试编写一个Jasmine单元测试,该测试将检查数组是否反转。 I have created a fiddle with my attempt and can see what is wrong, the current test is checking if it is equal but I want to be specific and check if it is reversed. 我用尝试创建了一个小提琴,可以看到哪里出了问题,当前的测试正在检查它是否相等,但是我想具体说明一下并检查它是否相反。 What is the correct way to implement this? 实施此方法的正确方法是什么?

http://jsfiddle.net/37qnmahk/4/ http://jsfiddle.net/37qnmahk/4/

Code: 码:

    $scope.reverse = function(array) {
        var copy = [].concat(array);
        return copy.reverse();
    };

Unit test: 单元测试:

describe('reverse', function() {
    it('passes if array is reversed', function() {
        var arr = [1, 2, 3];
        scope.reverse();
        scope.$digest();
        expect(arr).toEqual([3, 2, 1]);
    });
});

Actually in the test, after declaring the array, you should call your reverse function. 实际上,在测试中,在声明数组之后,应该调用反向函数。

describe('reverse', function() {
    it('passes if array is reversed', function() {
        var arr = [1, 2, 3];
        //call your reverse function here
        expect(arr).toEqual([3, 2, 1]);
    });
});

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

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