Js与Express

零碎

关于Mocha的test学习

下面是使用Mocha写的测试代码,用来测试一些数据是否正确,原来是这样的,一直重复写一些固定格式的东西,很繁琐,每次增加测试类都会重新写一遍,在维护上增加了很多难度。后面会做优化。

test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const chai = require("chai")
const axios = require("axios")
chai.Should();

describe("Sort via quick sort function.", ()=>{
it("正序", (done)=>{
axios.post("http://localhost:3000/quick",
[1,3,5,7,8,9,4,12,15,11,17,19]
).then((response)=>{
response.data.should.eql("1,3,4,5,7,8,9,11,12,15,17,19");
done();
}).catch((error)=>{
done(error);
})
})
it("乱", (done)=>{
axios.post("http://localhost:3000/quick",
[5,6,3234,8,7,54,3,223,2,23,4,5,5,4,23,23,23,23,4234,234,242,4,32423,423,42,42,3423,4]
).then((response)=>{
response.data.should.eql("2,3,4,4,4,4,5,5,5,6,7,8,23,23,23,23,23,42,42,54,223,234,242,423,3234,3423,4234,32423");
done();
}).catch((error)=>{
done(error);
})
})

it("倒", (done)=>{
axios.post("http://localhost:3000/quick",
[32423,4234,3423,3234,423,242,234,223,54,42,42,23,23,23,23,23,8,7,6,5,5,5,4,4,4,4,3,2]
).then((response)=>{
response.data.should.eql("2,3,4,4,4,4,5,5,5,6,7,8,23,23,23,23,23,42,42,54,223,234,242,423,3234,3423,4234,32423");
done();
}).catch((error)=>{
done(error);
})
})

it("重复数字多", (done)=>{
axios.post("http://localhost:3000/quick",
[1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,1,1,1,1,2,2,1,1]
).then((response)=>{
response.data.should.eql("1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2");
done();
}).catch((error)=>{
done(error);
})
})

it("length 0", (done)=>{
axios.post("http://localhost:3000/quick",
[]
).then((response)=>{
response.data.should.eql("");
done();
}).catch((error)=>{
done(error);
})
})

it("length 1", (done)=>{
axios.post("http://localhost:3000/quick",
[111]
).then((response)=>{
response.data.should.eql(111);
done();
}).catch((error)=>{
done(error);
})
})

it("alpha", (done)=>{
axios.post("http://localhost:3000/quick",
["b", "a", "c"]
).then((response)=>{
response.data.should.eql("a,b,c");
done();
}).catch((error)=>{
done(error);
})
})

it("null", (done)=>{
axios.post("http://localhost:3000/quick",
[null]
).then((response)=>{
response.data.should.eql("x");
done();
}).catch((error)=>{
done(error);
})
})
})

用到chai和should,axios

优化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const chai = require("chai")
const axios = require("axios")
chai.should();

var datatest = [
{ name: "1", input: { "inputtext": "ab-cd" }, output: "dc-ba" },
{ name: "null", input: { "inputtext": "" }, output: "" },
{ name: "random", input: { "inputtext": "a-bC-dEf-ghIj" }, output: "j-Ih-gfE-dCba" },
{ name: "2", input: { "inputtext": "Test1ng-Leet=code-Q" }, output: "Qedo1ct-eeLg=ntse-T" },
{ name: "3", input: { "inputtext": "Test1ng-Leet=code-Q!" }, output: "Qedo1ct-eeLg=ntse-T!" },
{ name: "0", input: { "inputtext": "Lets take LeetCode contest" }, output: "tset noce doCteeLe katsteL" },
{ name: "all", input: { "inputtext": "+_)*)(&&*^^*" }, output: "+_)*)(&&*^^*" }
]

function template(name, input, output) {
it(name, (done) => {
axios.post("http://localhost:3000/onlyword",
input).then((response) => {
response.data.should.eql(output);
done();
}).catch((error) => {
done(error);
})
})
}

describe("onlyword", () => {
for (var i = 0; i < datatest.length; i++) {
template(
datatest[i].name,
datatest[i].input,
datatest[i].output
)
}
})

routes code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var express = require("express");
var router = express.Router();

function uniqueMorseRepresentations(words) {
var morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
var strs = new Array;
var j = 0;
for (var i = 0; i < words.length; i++) {
var word = words[i].split("");
for (var m = 0; m < word.length; m++) {
if (word[m] >= 'a' && word[m] <= 'z') {
var num = word[m].charCodeAt() - 97;
var str = morse[num];
strs[j] += str;
}
}
j++;
}
if (strs) {
if (strs.length == 1)
return 1;
else {
var nstrs = new Array();
for (var k = 0; k < strs.length; k++) {
if (nstrs.indexOf(strs[k]) == -1)
nstrs.push(strs[k]);
}
return nstrs.length;
}
} else
return 0;
}

router.post('/', function (req, res, next) {
var tmp = req.body;
if (tmp == null) {
res.status(400).send("ERROR.");
return;
}
var tmp = JSON.parse(JSON.stringify(tmp));
var result = uniqueMorseRepresentations(tmp);
res.status(200).send("" + result);
});

module.exports = router;

在package.json文件中配置相关信息和依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"name": "cat",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"test": "cd test; mocha"
},
"dependencies": {
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"chai": "^4.2.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"mocha": "^6.1.4",
"morgan": "~1.9.1"
}
}

在script中可以自己设置关键字来简化操作,后面是操作符


创建express应用程序:

创建一个目录,在终端cd到这个文件下,express,然后yarn instill下载全部依赖,就可以了

关于test,需要引入mocha ,chai,should,mocha和chai要在package.json文件中添加依赖,并且运行测试需要在测试文件目录下运行mocha。

0%