一応、想定としては
を使っていて、アップロードすると、
{"files":[
{
"name":"small.jpg",
"size":15805,
"type":"image\/jpeg",
"url":"http:\/\/127.0.0.1:8765\/files\/small.jpg",
"thumbnailUrl":"http:\/\/127.0.0.1:8765\/files\/thumbnail\/small.jpg",
"deleteUrl":"http:\/\/127.0.0.1:8765\/index.php?file=small.jpg",
"deleteType":"DELETE"
}
]}
というようなjsonが返って来る、というような状態に今いる。
npm で mocha と supertest を入れる。
npm install -g mocha
npm install supertest --save-dev
test.js を作る。
tests/TestCases/scripts/test.js
var request = require('supertest')
describe('POST /uploads', function(){
it('respond with json', function(done){
request('http://127.0.0.1:8765')
.post('/uploads/upload')
.attach('files', 'tests/TestCase/Controller/_files/small.jpg')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200,
{files: [
{
name: 'small.jpg',
size: 79551,
type: 'image/jpeg',
url: 'http://127.0.0.1:8765/files/small.jpg',
thumbnailUrl: 'http://127.0.0.1:8765/files/thumbnail/small.jpg',
deleteUrl: 'http://127.0.0.1:8765/index.php?file=small.jpg',
deleteType: 'DELETE'
}
]}
, done);
});
});
supertestのgithubに書いてあるけど、
.expect(status, body[, fn])
というように使うので、bodyの部分には期待するjson出力をそのまま入れる。
サーバーにpostした結果がきちんとしたjsonになってて、content-typeヘッダーも
application/json; charset=UTF-8
になっていれば、bodyにはjsonがそのまま入るって仕組み。
直接 mocha でテスト実行
mocha tests/TestCase/scripts/test.js
grunt から実行。
最初にgrunt-simple-mocha入れて、
npm install grunt-simple-mocha --save-dev
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
simplemocha: {
options: {
ui: 'bdd',
reporter: 'nyan'
},
all: { src: ['tests/TestCase/scripts/**/*.js'] }
},
});
grunt.loadNpmTasks('grunt-simple-mocha');
grunt.registerTask('mocha', ['simplemocha']);
};
実行する。
grunt mocha
Running "simplemocha:all" (simplemocha) task
0 -__,------,
1 -__| /\_/\
0 -_~|_( x .x)
-_ "" ""
0 passing (1s)
1 failing