循环中的嵌套单元测试用例总是成功的

时间:2015-04-21 01:28:38

标签: angularjs jasmine karma-runner yeoman karma-jasmine

我重复了从重复行到循环的测试用例。但无论我在testdata对象中放置什么值,测试总是正确的。我错过了什么?

it('should calculate the correct price using the datetimepicker', function() {
    var testdata = {
    bundesweit: {
      sonder: '26.00',
      mini: '24.00',
      klein: '48.30',
      kompakt: '57.70',
      mittel: '66.90',
      transporter: '75.90'
    },
    lokal: {
      sonder: '18.35',
      mini: '18.35',
      klein: '23.85',
      kompakt: '28.35',
      mittel: '42.40',
      transporter: '42.40'
    }
  };

  var testcase = function (tariff, carClass, expectedPrice) {
    it('distance: ' + scope.rental.distance +
       ', tariff: ' + tariff + 
       ', carClass: ' + carClass, function () {
      scope.rate.tariff = tariff;
      scope.rate.carClass = carClass;
      expect(scope.price().toFixed(2)).toEqual(expectedPrice);
    });
  };

  for (var tariff in testdata) {
    for (var carClass in testdata[tariff]) {
      var expectedPrice = testdata[tariff][carClass];
      testcase(tariff, carClass, expectedPrice);
    }
  }
});

1 个答案:

答案 0 :(得分:1)

看起来你好像这样......

require("should");
describe("test", function() {
    it('should pass', function(){

        "a".should.equal("a");

        var f = function() {
            it('should fail', function() {
                "a".should.equal("b");
            })
        };

        var i = 10;
        while(i--){
            f();
        }
    })
});

你应该这样做......

describe("test", function() {
    describe('subsection', function(){

        var f = function() {
            it('should fail', function() {
                "a".should.equal("b");
            })
        };

        var i = 10;
        while(i--){
            f();
        }
    })
});

问题是it函数是在其他it次调用中调用的。