【问题标题】:How to write a test case for my code如何为我的代码编写测试用例
【发布时间】:2017-02-24 11:55:38
【问题描述】:

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

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

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"])

【问题讨论】:

    标签: javascript unit-testing testing jasmine


    【解决方案1】:

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

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

    现在...
    试着忘记你的实现细节,把一个单一的需求当作一个可销售的产品。

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

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

    Foo 先生何时接受我的产品? --> 接受标准是什么?

    1. 第一个要求是:

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

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

    1. 第二个要求是:

    greet 函数应该返回“你好,” + name + “。” 什么时候 strong> 使用小写字符串参数调用。

    Foo 先生知道调用 greet("tom") 应该会收到 "Hello, Tom.",如果是这样,他会接受该产品。
    Foo 先生,他不是一个肤浅的买家,在接受产品之前会致电greet("jack")greet("tOM")
    Foo先生也是个狡猾的人,他会调用greet("TOM")来验证产品不会产生"Hello, TOM."字符串。
    这就是所谓的测试粒度!!

    1. 第三个要求是:

    greet 函数应该返回 "HELLO, " + NAME +"!" 何时 strong> 使用大写字符串调用。

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

    这里是 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"/>

    【讨论】:

    • 就表述而言,我看到您在每个人类句子中都使用“应该”。有哪些做法?是否可以从一个主题开始(比如,切割函数、解析器……测试的东西)?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 2023-01-10
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多