【问题标题】:JS unit test with Jasmine使用 Jasmine 进行 JS 单元测试
【发布时间】:2014-03-21 10:59:10
【问题描述】:

我对 Jasmine 很陌生,事实上我今天才开始并且没有编写 JS 单元测试用例的先验知识。我正在尝试在 Jasmine 中编写一个简单的测试用例,以测试一个非常基本的 JS 代码。我在我的项目中添加了所有必要的脚本和库。

所以这是一段 JS 代码。我正在获取澳大利亚当地的日期和时间,并以特定格式排列它们。

var date = {

    formatFullDate: function(date) {
        var localDate = this.getLocalTimeFromAustraliaTime(date),
            month = this.addZeroToFront(localDate.getMonth() + 1),
            hour = this.addZeroToFront(localDate.getHours()),
            minute = this.addZeroToFront(localDate.getMinutes());

        return localDate.getDate() + '/' + month + '/' + localDate.getFullYear() + ' ' + hour + ':' + minute;
    },

    formatTime: function(date) {
    var localDate = this.getLocalTimeFromAustraliaTime(date),
        hour = this.addZeroToFront(localDate.getHours()),
        minute = this.addZeroToFront(localDate.getMinutes());

    return hour + ':' + minute;
    },

    addZeroToFront: function(whatever) {
    if (whatever < 10) whatever = "0" + whatever;
    return whatever;
    },

    getUTCtimeOffset: function() {
    var date = new Date();
    return date.getTimezoneOffset();
    },

    getLocalTimeFromAustraliaTime: function (date) {
    var utcTime = new Date(date.getTime() - 11*60*60*1000),
        localDate = new Date(utcTime - this.getUTCtimeOffset()*60*1000);
    return localDate;
    }
}

在上面提到的代码中,我可以测试各种事情,例如函数正在获取正确的时区、在时间之前添加 0、格式化日期等。

我想知道如何构建我的测试用例。我能想到可能的结构

describe( "Australian full date format", function () {

    describe( "Time format", function () {
        it("Check if time format is fetched correctly", function () {
            expect(something).toEqual(something);
        });
    });

    describe( "Adding 0 to the front", function () {
        it("Check if 0 is added prior to the time", function () {
            expect(something).toEqual(something);
        });
    });

    describe( "Get local Australian time", function () {
        it("Check if correct Australian time is fetched", function () {
            expect(something).toEqual(something);
        });
    });

    it("Check if the date is formatted correctly", function () {
        expect(something).toEqual(something);
    });
});

如果我的方向是正确的,那么我该如何从这一点开始前进。我很困惑如何为我的 JS 代码编写等效的测试用例。

【问题讨论】:

    标签: javascript jquery unit-testing jasmine


    【解决方案1】:

    我会创建类似这样的测试:

    describe("Australian full date format", function() {
      describe("#formatFullDate", function() {
        it("should have format 'date/month/year hour:min'", function() {
          //...
        });
      });
    
      describe("#formatTime", function() {
        it("should have format 'hour:min'", function() {
          //...
        });
      });
    
      describe("#addZeroToFront", function() {
        describe("if whatever < 10", function() {
          it("should add 0 to front", function() {
            //...
          });
        });
        describe("if whatever >= 10", function() {
          it("should return original without any changes", function() {
            //...
          });
        });
      });
    
      //...
    });
    

    所以我使用describe 来显示正在测试的功能。然后使用describe 作为条件。使用it 仅测试功能的一个特性。所有外部对象(在这种情况下为Date)都应该被存根以测试函数getUTCtimeOffsetgetLocalTimeFromAustraliaTime

    【讨论】:

    • 谢谢。你能给我一个例子,我会在测试用例中实际使用日期对象来运行测试吗?
    • hm,您可以在测试中使用您的日期对象,例如date.formatFullDate(testDate)
    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多