3

我正在 src 文件夹中使用 moment.js 做一个小 Jasmine-standalone-demo。

describe("moment.js -> diff", function() {
    var start, end;
    it("leap year", function() {
        start = moment([2012, 1, 26]);
        end = moment([2012, 2, 4]);
        expect( end.diff(start, "days") ).toEqual(7);
    });
    it("winter-/summertime", function() {
        start = moment("2012-10-28T02:00:00+02:00");
        end = moment("2012-10-28T03:00:00+01:00");
        expect( end.diff(start, "hours") ).toEqual(0);
    });
});

第二个测试预计会失败,它确实会失败(diff 返回 1),但是 Jasmine 给出了一个堆栈跟踪,这对我来说几乎没用,除非 Jasmine 本身有错误:

Error: Expected 1 to equal 0.
    at new jasmine.ExpectationResult (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:114:32)
    at null.toEqual (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:1235:29)
    at null.<anonymous> (http://localhost/jasmine/spec/MomentSpec.js:11:38)
    at jasmine.Block.execute (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:1064:17)
    at jasmine.Queue.next_ (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2096:31)
    at jasmine.Queue.start (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2049:8)
    at jasmine.Spec.execute (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2376:14)
    at jasmine.Queue.next_ (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2096:31)
    at onComplete (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2092:18)
    at jasmine.Spec.finish (http://localhost/jasmine/lib/jasmine-1.3.1/jasmine.js:2350:5)

有人可以启发单元测试新手。也许我做错了什么。

4

1 回答 1

3

堆栈跟踪会告诉您哪个expect()函数失败,以防您在一个块中有很多expect()s 。it()

正如@zbynour 提到的,你必须去你的第 11 行,第 38 个字符,MomentSpec.js找出答案!

于 2013-01-17T02:16:34.170 回答