Node.js を使用して関数テストの実装方法_JavaScript スキルを実行する

WBOY
リリース: 2016-05-16 17:18:53
オリジナル
1339 人が閲覧しました

情報
先週の会議で、同僚は現在 Java を使用して関数テストを作成していると述べましたが、その結果、冗長なコードが大量に生成され、プロジェクト全体が肥大化してきました。ここで、機能テストを迅速に構築するための単純なテンプレート プロジェクトが緊急に必要です。

後で戻って考えてみましたが、なぜ関数テストを行うのに Java を使用する必要があるのでしょうか?

Node.js は良い選択であり、json を自然にサポートしているため、github に戻って検索したところ、確かに関連プロジェクト testosterone があったので、このブログを思いつきました。

サーバー
デモを実行するには、それをサポートする対応するサーバーが必要になるのは当然です。

ここではサーバーとして Express を選択します。

まず、サーバー フォルダーを作成し、新しい package.json を作成します。

コードをコピー コードは次のとおりです:

{
"name": " wine-cellar ",
"description": "ワインセラー アプリケーション",
"バージョン": "0.0.1",
"プライベート": true,
"依存関係": {
"express ": "3.x"
}
}

次にコマンドを実行します

コードをコピー コードは次のとおりです:

npm install

Express がインストールされました。

実験用にいくつかの単純な get post メソッドを実装します

コードをコピー コードは次のとおりです。

var Express = require('express')
、app =express();

app.use(express.bodyParser());

app.get('/hello', function(req, res) {
res.send("hello world");
});

app.get('/ ', function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end() ;
}, 200);
});

app.get('/hi', function (req, res) {
if (req.param('hello') !== 未定義) {
res.writeHead(200, {'Content -Type': 'text/plain'});
res.end('Hello!');
} else {
res.writeHead(500, {'Content-Type': 'text/プレーン'});
res.end('代わりに投稿を使用');
}
});

app.post('/hi', function (req, res) {
setTimeout(function () {
res.writeHead(200, {'Content-Type': 'text/plain'} );
res.end(req.param('メッセージ') || 'メッセージ');
}, 100);
});


app.get('/user', function(req, res) {
res.send(
[
{name:'jack'},
{name: 'トム'}
]
);
});

app.get('/user/:id', function(req, res) {
res.send({
id: 1,
name: "node js",
説明: "私はノード js"
});
});

app.post('/user/edit', function (req, res) {
setTimeout(function () {
res.send({
id:req.param('id' ),
status:1
});
}, 100);
});
app.listen(3000);
console.log('ポート 3000 でリッスンしています...');

テストステロン
サーバーをセットアップしたら、当然のことながらテストを開始します。

このプロジェクトのインターフェースは非常に洗練された名前が付けられており、コードに直接ロードできます。

まず、基本的な関数をテストします

コードをコピーします コードは次のとおりです:

var testosterone = require('testosterone')({port: 3000})
、assert = testosterone.assert;

テストステロン
.get('/hello',function(res){
assert.equal(res.statusCode, 200);
})

.get('/hi',function(res){
assert.equal(res.statusCode, 500);
})

.post('/hi', {data: {message: 'hola'}}, {
status: 200
,body: 'hola'
});

次に、上でシミュレートしたユーザーの投稿の取得に対して簡単なテストを実行します。

コードをコピー コードは次のとおりです:

var testosterone = require('testosterone') ({ポート : 3000})
、assert = testosterone.assert;

テストステロン
.get('/user', function (res) {
var ExpectRes = [
{name:'jack'},
{name:'tom'}
];

assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)),JSON.stringify(expectRes));
})

.get('/user/1', function (res) {

var user = JSON.parse(res.body);

assert.equal(res.statusCode, 200);
assert.equal(user.name, "node js");
assert.equal(user.description, "私はnode js");
})

次に、give when then を使用して各テスト ケースを記述したい場合は、次のようにすることができます:

コードをコピー コードは次のとおりです:

var testosterone = require('testosterone') ({ポート : 3000, タイトル: 'テスト ユーザー API'})
、add = testosterone.add
、assert = testosterone.assert;

testosterone
.add(
'/user/{id} n にユーザー ID を与える'
'応答 user n がある場合'
'THEN はユーザー json を返すはずです',

function (cb) {
testosterone.get('/user/1', cb(function (res) {
var ExpectRes = {
id: 1,
name: "node js",
説明: "私はノード js"
};

assert.equal(res.statusCode, 200);
assert.equal(JSON.stringify(JSON.parse(res.body)), JSON.stringify(expectRes));
}));
})


.add(
'ユーザー情報を /user/edit n に POST したとき'
'検索の変更が成功したとき n'
'THEN はステータス 1 を返すはずです',

function (cb) {
testosterone.post('/user/edit', {data: {id: 1, name: "名前変更"}}, cb(function (res) {
var res = JSON.parse(res.body);
assert.equal(res.status, 1);
}));
}
)

.run(function () {
require('sys').print('done!');
});

結論
上記のコードを通して、testosterone は Java の長い http ヘッダー設定よりも実際にはるかに単純で洗練されていることがわかります。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!