如何为我的代码编写测试用例

时间:2017-02-24 11:55:38

标签: javascript unit-testing testing jasmine

我有没有办法为我的代码编写测试用例?在使用测试套件测试之前,我是否需要删除任何内容?

我是测试新手,想知道我可以测试下面的代码。

var hello = "Hello, ";

function greet(name){

//Requirement UpperCase
function upperCase(){
if (name.toUpperCase() === name) { //uppercase string
  console.log(hello.toUpperCase() + name + '!');
} 
else {
  console.log(hello + name + ".");
}

}
//Requirement last element
function namesArray(){
if (name.length > 1){
var lastElement = name.pop();
console.log(hello + name + " and " + lastElement + ".");
}

else{
  console.log(hello + name + ".");
}

}

//Comparing name//

if (name == null) {
console.log(hello + "my friend.")
}
else if(typeof name === 'string'){//will accept strings and return them.
upperCase();
}
else if (Array.isArray(name)){//will accept arrays and return them.
namesArray();
}

}

greet()
greet("James")
greet("Ruth");
greet(["Barry", "Kate"])
greet(["Kerry", "Mike", "Snake", "FOX"])

2 个答案:

答案 0 :(得分:3)

通常在编写业务要求之前编写测试,避免在编写实现时考虑实现。

首先,您的实现无法测试,因为您的函数严格依赖于对象 console 。 在某些时候,在期望阶段,您需要一个输出用作比较器,在您的实现中,这意味着修改从 greet <返回字符串或对象(如greetResult = {message:"Hello, "})的代码/ em>功能。

现在......
尽量忘记您的实施细节,并将单一要求视为可销售的产品。

您的客户(Foo先生)不想知道您的代码详情,但他想要的产品符合要求的要求。

所以问问自己(对所有要求都这样做):

当Foo先生接受我的产品时? - &gt; 哪些是验收标准?

  1. 第一个要求是:

  2.   

    greet函数 应该 返回“你好,我的朋友。” 不带参数调用

    先生。 Foo知道调用greet()应该收到"Hello, my friend.",在这种情况下 接受 产品。 (在现实世界中并非总是如此

    1. 第二个要求是:

    2.   

      greet函数 应该 返回“Hello,”+ name +“。” 使用小写字符串参数调用

      Foo先生知道致电greet("tom")应该收到"Hello, Tom.",如果是, 接受 产品。
      Foo先生,这不是一个肤浅的买家,会在 接受 产品之前致电greet("jack")greet("tOM")
      此外,Foo先生是个狡猾的人会打电话greet("TOM")来验证产品是否产生"Hello, TOM."字符串。
      这称为测试粒度!!

      1. 第三个要求是:

      2.   

        greet函数 应该 返回“HELLO”,+ NAME +“!” 使用大写字符串调用

        等等...... 通过这种方式,您将以积极的方式测试代码,并且需要覆盖边界情况。

        这里 jasmine 测试:

        // source code
        
        
        var greet = function (name){
          var hello = "Hello, ";
        
        //Requirement UpperCase
          function upperCase(){
            if (name.toUpperCase() === name) { //uppercase string
              return hello.toUpperCase() + name + '!';
            } 
            else {
              return hello + name + ".";
            }
          }
          
        //Requirement last element
          function namesArray(){
            if (name.length > 1){
              var lastElement = name.pop();
              return hello + name + " and " + lastElement + ".";
            } 
            else{
               return hello + name + ".";
            }
          }
        
        //Comparing name//
        
          if (name == null) {
            return hello + "my friend.";
          }
          else if(typeof name === 'string'){
          //will accept strings and return them
            return upperCase();
          }
          else if (Array.isArray(name)){
            return namesArray();
          }
          else { 
            throw new Error("");
          }
        
        }
         
        
        // test code
        describe("greet", function() {
            
          it("should return Hello, my friend. when name is null", function() {
            var act = greet();
            expect(act).toBe("Hello, my friend.");
            console.log(act);
          });
         
          it("should return Hello, name. when called with lower case", function() {
            var act = greet("tom");
            expect(act).toBe("Hello, tom.");
            console.log(act);
        
            act = greet("jack");
            expect(act).toBe("Hello, jack.");
        
            act = greet("TOM");
            expect(act).not.toBe("Hello, tom.");
          });
          
          it("should return HELLO, NAME! when called with upper case  string", function() {
            var act = greet("TOM");
            expect(act).toBe("HELLO, TOM!");
            console.log(act);
        
            act = greet("JACK");
            expect(act).toBe("HELLO, JACK!");
        
            act = greet("tOM");
            expect(act).not.toBe("HELLO, TOM!");
        
          });
        
          it("should return Hello, names and last name. when called with an array of strings", function() {
            
            var act = greet(["TOM","jack"]);
            expect(act).toBe("Hello, TOM and jack.");
            console.log(act);
        
            act = greet(["TOM","jack","giulia"]);
            expect(act).toBe("Hello, TOM,jack and giulia.");
          
          });
        
        
          it("should throw an exception when called with invalid arguments",function(){
            
             expect( function(){ greet({}); } ).toThrow();
             expect( function(){ greet(1); } ).toThrow();
             
             var functionArgument = function(){
                     //... do nothing
             };
             expect( function(){ greet(functionArgument); } ).toThrow();
           
           // uncomment and cover the following case
           // expect( function(){ greet(undefined); } ).toThrow();
        
          });
        });
         
        // load jasmine htmlReporter
        (function() {
          var env = jasmine.getEnv();
          env.addReporter(new jasmine.HtmlReporter());
          env.execute();
        }());
        <title>Jasmine Spec Runner</title>
        
        <script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
        <script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
        <link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet"/>

答案 1 :(得分:-2)

你应该先添加分号。否则看起来很好。

greet();
greet("James");
greet("Ruth");
greet(["Barry", "Kate"]);
greet(["Kerry", "Mike", "Snake", "FOX"]);