有没有办法为我的代码编写测试用例?在使用测试套件对其进行测试之前,我是否需要删除任何内容?
我是测试新手,想知道我可以测试下面的代码。
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"])
最佳答案
通常应该在编写业务需求之前编写测试,避免在编写测试时考虑到实现。
首先,无法测试您的实现,因为您的功能严格依赖于对象console。
在某些时候,在预期阶段,您需要一个输出用作比较器,在您的实现中,这意味着修改返回字符串或对象的代码(如 greetResult = {message:"Hello, "}
) 来自 greet 函数。
现在……
尽量忘记您的实现细节,将单个需求视为可销售的产品。
您的客户(Foo 先生)不想知道您的代码详细信息,但他想要一个符合要求的产品。
所以问问自己(对所有要求都这样做):
Mr. Foo 何时接受我的产品? --> 验收标准是什么?
第一个要求是:
the
greet
function should return "Hello, my friend." when is called without parameters.
先生。 Foo 知道调用 greet()
应该接收到 “Hello, my friend.”
并且在这种情况下将接受产品。 (在现实世界中并不总是如此)
第二个要求是:
the
greet
function should return "Hello, " + name + "." when is called with a lower case string parameter.
Mr Foo 知道调用 greet("tom")
应该接收到 "Hello, Tom."
并且如果是这样的话 accept 产品。
Foo 先生不是肤浅的买家,他会在 接受 产品。
此外,Mr. Foo 是一个狡猾的人,他会调用 greet("TOM")
来验证产品不会生成 "Hello, TOM."
字符串。
这就是所谓的测试粒度!!
第三个要求是:
the
greet
function should return "HELLO, " + NAME +"!" when is called with an upper case string.
等等...
通过这种方式,您正在以积极的方式测试您的代码,并且您需要涵盖边界情况。
这里是 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"/>
关于javascript - 如何为我的代码编写测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42438018/