茉莉花无法读取属性'长度'未定义的

时间:2017-05-25 10:01:45

标签: javascript unit-testing testing jasmine

你是单元测试的新手我已经编写了一个程序,它接受3个参数,两个romanletters和一个运算符(+, - ,/,*)它应该执行操作,它应该返回romanLetters中的值我试图写一个单元测试该问题,它说无法读取未定义的属性长度我不知道为什么发生我的代码运行正常。我在这里附上我的代码

Mainjs文件

var main=function(operand1,operand2,operator) {
    var concat,number;
    var operand1=romantonumerals(operand1);
    var operand2=romantonumerals(operand2);
    //var operator=operatorconversion(operator);
    console.log("operand1 "+operand1);
    console.log("operand2 "+operand2);
    if(operand1>operand2) {
      concat=operand1+operator+operand2;
    }
    else {
      concat=operand2+operator+operand1;
    }
    number=Math.round(eval(concat));
    console.log(number);
    if(number<500) {
      result=numeralstoroman(number)
    }
    else if(number===0) {
      result="Make sure its greater than 0";
    }
    else {
      result="Make sure your number gets less than 500 try with some other romans";
    }
    console.log(result);
    return result;
  };
  var numeralstoroman=function(number) {
    var roman="",temp;
    var ones=function(number) {
      switch (number) {
        case 1:
          roman+="I";
          break;
        case 2:
          roman+="II";
          break;
        case 3:
          roman+="III";
          break;
        case 4:
          roman+="IV";
          break;
        case 5:
          roman+="V";
          break;
        case 6:
          roman+="VI";
          break;
        case 7:
          roman+="VII";
          break;
        case 8:
          roman+="VIII";
          break;
        case 9:
          roman+="IX";
          break;
      }
    };
    var twos=function(number) {
      if(number>=10 && number<20) {
        roman+="X";
        ones(number%10);
      }
      else if(number>=20 && number30) {
        roman+="XX";
        ones(number%10);
      }
      else if(number>=30 && number<40) {
        roman+="XXX";
        ones(number%10);
      }
      else if(number>=40 && number<50) {
        roman+="XL";
        ones(number%10);
      }
      else if(number>=50 && number<60) {
        roman+="L";
        ones(number%10);
      }
      else if(number>=60 && number<70) {
        roman+="LX";
        ones(number%10);
      }
      else if(number>=70 && number<80) {
        roman+="LXX";
        ones(number%10);
      }
      else if(number>=80 && number<90) {
        roman+="LXXX";
        ones(number%10);
      }
      else if(number>=90 && number<100) {
        roman+="XC";
        ones(number%10);
      }
    };
    var threes=function(number) {
      if(number>=100 && number<200) {
          roman+="C";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=200 && number<300) {
          roman+="CC";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=300 && number<400) {
          roman+="CCC";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=400 && number<500) {
          roman+="CD";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
    }
    if(number<10) {
      ones(number);
    }
    else if(number>=10 && number<100) {
      twos(number);
    }
    else if(number>=100 && number<500) {
      threes(number);
    }
    console.log(roman);
    return roman;
  };
  var character=function(str) {
    switch(str) {
      case "I":
        return 1
      case "V":
        return 5
      case "X":
        return 10
      case "L":
        return 50
      case "C":
        return 100
      case "D":
        return 500
      default:
        return -1;
    }
  }
  var romantonumerals=function(operand) {
    var no=0,i=0,char1="",char2="";
    while(i<operand.length) {
      char1=character(operand[i]);
      if(i+1<operand.length) {
        char2=character(operand[i+1]);
        if(char1>=char2) {
          no+=char1;
          i++;
        }
        else {
          no=no+(char2-char1);
          i+=2;
        }
      }
      else {
        no+=char1;
        i++;
      }
    }
    return no;
  };
  var ans=main("V","I","+");
  console.log(ans);

JasmineHTML FIle

<!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>Jasmine Spec Runner v2.2.0</title>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
    <link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">

    <script src="lib/jasmine-2.2.0/jasmine.js"></script>
    <script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
    <script src="lib/jasmine-2.2.0/boot.js"></script>

    <!-- include source files here... -->
    <script src="romtantonumeralstoroman.js"></script>
    <!-- include spec files here... -->
    <script src="spec.js"></script>


  </head>

  <body>
  </body>
  </html>

Jasmine测试文件

describe("A suite", function() {
it("contains spec with an expectation", function() {
    main();
    expect(main("V","I","+").toBe("VI"));
});
});

JSFIDDLE链接

https://jsfiddle.net/pLecneto/

1 个答案:

答案 0 :(得分:0)

问题是您在规范中没有任何参数的情况下调用main();。删除它,因为当操作数为while(i<operand.length)时,它会在undefined处抛出异常。

并更改您的expect语句,如:

expect(main("V","I","+")).toBe("VI");