Jasmine and Requirejs in Resharper 7

前端 未结 3 767
情话喂你
情话喂你 2021-01-13 15:28

I\'m trying to run some JavaScript code using jasmine and Resharper 7 within visual studio 2012. I follow the AMD pattern with the aid of requirejs. However, i haven\'t mana

3条回答
  •  一向
    一向 (楼主)
    2021-01-13 16:05

    A simplified version of mgsdev response, put the loading code in a beforeEach statement, that way you can write short expectations.

    module:

    define("stringCalculator", function () {
        return {
            calculate: function (string) {
                var result = 0;
                string.split("+").forEach(function(number) {
                    result += parseInt(number);
                });
                return result;
            }
        };
    });
    

    test file: if calculator is undefined it will try to load it before every expectation.

    /// 
    /// 
    
    describe("string calculator", function () {
        var calculator;
    
        beforeEach(function () {    
            if (!calculator) {
                require(["stringCalculator"], function (stringCalculator) {
                    calculator = stringCalculator;
                });
    
                waitsFor(function () {
                    return calculator;
                }, "loading external module", 1000);
            }
        });
    
        it("should add 1 and 2", function () {
            var result = calculator.calculate("1+2");
            expect(result).toEqual(3);
        });
    
        it("should add 2 and 2", function () {
            var result = calculator.calculate("2+2");
            expect(result).toEqual(4);
        });
    
        it("should add 1, 2 and 3", function() {
            var result = calculator.calculate("1+2+3");
            expect(result).toEqual(6);
        });
    });
    

提交回复
热议问题