如何使用 supertest 和 jasmine-node 测试 PUT 方法
How to Test PUT method using supertest and jasmine-node
我正在使用 expressjs 构建一个 API,我的路线如下所示
module.exports = function(app){
var book = require('../controllers/book.controller');
app.get('/api/books', book.getBooks); //get all books
app.post('/api/books', book.addBook); //add a book
app.put('/api/book/:book_id', book.updateBook); //update a book entry
app.delete('/api/book/:book_id', book.deleteBook); //delete a book
}
回调函数定义如下
var Book = require('../models/book.model');
module.exports = {
addBook: function(req, res){
Book.create(req.body, function(err, book){
if(err){
return res.json(400, err);
}
res.status(201).json(book);
});
},
getBooks: function(req, res){
Book.find({}).exec(function(err, books){
if(err){
return res.json(400, err);
}
res.status(201).json(books);
});
},
getOneBook: function(req, res){
Book.findById({_id: req.params.book_id}, function(err, book){
if(error){
return res.json(400, err)
}
res.status(201).json(book);
})
},
updateBook: function(req, res){
Book.update({_id: req.params.book_id}, req.body, function(err, book){
if(err){
return res.json(400, err);
}
res.status(201).json(book);
});
},
deleteBook: function(req, res){
Book.remove({_id: req.params.book_id}, function(err, book){
if(err){
return res.json(400, err);
}
res.status(200).json(book);
})
}
};
我正在使用 jasmine-node 和 supertest 测试路由,这是我目前所做的
var app = require('../app.js');
var request = require('supertest')(app);
describe('Test for book route', function(){
it('Test GET method for /api/books', function(done){
request
.get('/api/books')
.expect(201)
.expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){
if(err){
return done(err);
}
done();
});
});
it('Test POST method for /api/books', function(done){
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
request
.post('api/books')
.send(book)
.expect(200)
done();
});
//THIS IS WHERE THE ISSUE IS
it('Test PUT method for /api/book/:book_id', function(done){
var bookEdit = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography by the only Nigerian Nobel Laureate'};
request
.put('api/book/:book_id')
.send(bookEdit)
.expect(200)
.done();
});
});
获取 book_id 变量的最佳方法是什么,以便我可以编辑使用 PUT 测试创建的条目?
您需要将图书的 ID 存储到用于创建新书的变量中,然后将其作为您尝试更新的图书的 ID 传递。
这意味着您需要将 book 变量放在 it 函数之外,以便其他 it 函数可以访问它。
post it 函数也需要访问响应以获取返回的图书内容。
这是需要更新的测试代码部分:
...
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
...
it('Test POST method for /api/books', function(done){
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
request
.post('api/books')
.send(book)
.expect(200)
.end(function(err, res){
if(err){
return done(err);
}
book = res.body;
done();
});
});
it('Test PUT method for /api/book/:book_id', function(done){
var bookEdit = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography by the only Nigerian Nobel Laureate'};
request
.put('api/book/' + book._id)
.send(bookEdit)
.expect(200)
.done();
});
我正在使用 expressjs 构建一个 API,我的路线如下所示
module.exports = function(app){
var book = require('../controllers/book.controller');
app.get('/api/books', book.getBooks); //get all books
app.post('/api/books', book.addBook); //add a book
app.put('/api/book/:book_id', book.updateBook); //update a book entry
app.delete('/api/book/:book_id', book.deleteBook); //delete a book
}
回调函数定义如下
var Book = require('../models/book.model');
module.exports = {
addBook: function(req, res){
Book.create(req.body, function(err, book){
if(err){
return res.json(400, err);
}
res.status(201).json(book);
});
},
getBooks: function(req, res){
Book.find({}).exec(function(err, books){
if(err){
return res.json(400, err);
}
res.status(201).json(books);
});
},
getOneBook: function(req, res){
Book.findById({_id: req.params.book_id}, function(err, book){
if(error){
return res.json(400, err)
}
res.status(201).json(book);
})
},
updateBook: function(req, res){
Book.update({_id: req.params.book_id}, req.body, function(err, book){
if(err){
return res.json(400, err);
}
res.status(201).json(book);
});
},
deleteBook: function(req, res){
Book.remove({_id: req.params.book_id}, function(err, book){
if(err){
return res.json(400, err);
}
res.status(200).json(book);
})
}
};
我正在使用 jasmine-node 和 supertest 测试路由,这是我目前所做的
var app = require('../app.js');
var request = require('supertest')(app);
describe('Test for book route', function(){
it('Test GET method for /api/books', function(done){
request
.get('/api/books')
.expect(201)
.expect('Content-Type', 'application/json; charset=utf-8')
.end(function(err, res){
if(err){
return done(err);
}
done();
});
});
it('Test POST method for /api/books', function(done){
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
request
.post('api/books')
.send(book)
.expect(200)
done();
});
//THIS IS WHERE THE ISSUE IS
it('Test PUT method for /api/book/:book_id', function(done){
var bookEdit = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography by the only Nigerian Nobel Laureate'};
request
.put('api/book/:book_id')
.send(bookEdit)
.expect(200)
.done();
});
});
获取 book_id 变量的最佳方法是什么,以便我可以编辑使用 PUT 测试创建的条目?
您需要将图书的 ID 存储到用于创建新书的变量中,然后将其作为您尝试更新的图书的 ID 传递。
这意味着您需要将 book 变量放在 it 函数之外,以便其他 it 函数可以访问它。 post it 函数也需要访问响应以获取返回的图书内容。
这是需要更新的测试代码部分:
...
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
...
it('Test POST method for /api/books', function(done){
var book = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography'};
request
.post('api/books')
.send(book)
.expect(200)
.end(function(err, res){
if(err){
return done(err);
}
book = res.body;
done();
});
});
it('Test PUT method for /api/book/:book_id', function(done){
var bookEdit = {title: 'Ake', author: 'Wole Soyinka', blurb: 'An autobiography by the only Nigerian Nobel Laureate'};
request
.put('api/book/' + book._id)
.send(bookEdit)
.expect(200)
.done();
});