简体   繁体   中英

How to write a test case for my code

Is there a way for me to write a test case for my code? Do i need to remove anything before i test it using a test suite?

I am new to testing and would like to know i can test the code below.

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

Usually tests should be written before coding the businness requirements avoiding to write the tests with an implementation in mind.

First of all your implementation can't be tested because your function strictly depends on the object console . At some point, during the expectation phase, you'll need an output to use as comparator and in your implementation this it means modify the code returning a string or an object (like greetResult = {message:"Hello, "} ) from the greet function.

Now ...
Try to forget your implementation details and think about a single requirement as a salable product.

Your customer (Mr. Foo) doesn't want to know your code details, but he wants a product that matches the asked requirement.

So ask to yourself (and do it the same for all requirements):

When Mr. Foo accepts my product ? --> Which are the acceptance criteria ?

  1. The first requirement is:

the greet function should return "Hello, my friend." when is called without parameters.

Mr. Foo knows that calling greet() should receives "Hello, my friend." and in that case will accept the product. ( not always true in the real world )

  1. The second requirement is:

the greet function should return "Hello, " + name + "." when is called with a lower case string parameter.

Mr Foo knows that calling greet("tom") should receives "Hello, Tom." and if so will accept the product.
Mr. Foo it' s not a superficial buyer and will calls greet("jack") or greet("tOM") before accept the product.
Also Mr. Foo is a foxy man will calls greet("TOM") to verify that the product does'nt produce "Hello, TOM." string.
This is know as test granularity !!

  1. The third requirement is:

the greet function should return "HELLO, " + NAME +"!" when is called with an upper case string.

And so on ...
In this way you are testing your code in a positive way and you need to cover the boundary cases.

Here the jasmine tests:

 // 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"/> 

you should add semicolons first. Otherwise seems fine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM