被测方法出现在单元测试中而不是在动作中
Method under test appearing in unit test other than in the action
我想知道是否有人可以给我一些关于设计我的单元测试的指示,特别是被测试的功能出现在测试本身而不是出现在单元操作中的地方。
我正在用节点编写服务器端 javascript 并使用 rsvp 异步调用 restful 后端。
我的单元测试结构如下。
//Setup
//Action
//Assertion
//Cleanup
现在,我正在构建一个异步删除数据库中条目的函数,因此我的单元测试如下所示。
"deletingFunction_1entry_returns1EntryDeleted": function() {
return new rsvp.Promise(function( resolve, reject ) {
//Setup
var DatabaseEntry = { "Info": "Info" };
setup( DatabaseEntry ).then(function( xPostedEntries ) {
//if the setup promise resolves then...
//Action
xDelete( xPostedEntries ).then(function( xDeletedEnteries ) {
//Assertion
if ( xPostedEntries === xDeletedEntries ) {
//if the posted enteries matched the deleted enteries then...
resolve();
} else {
//otherwise the posted enteries did not match the deleted
//enteries so...
reject("The posted enteries did not match the deleted enteries.");
}
});
}, function( xPostedEnteries ) {
//otherwise the setup promise did not resolve so...
//Cleanup
/***************************************************************/
/* Here's where I'm having trouble. If the setup fails to post */
/* all of the entries to the database then the test is a */
/* scratch but I want to clean up all the posts that 'were' */
/* posted but to remove them from the database, I'd be using a */
/* deleting function, which is the function that I'm testing. */
/* So how can I redesign my unit test not to use the function */
/* that it's testing. */
/***************************************************************/
reject("The setup failed.");
});
});
}
已编辑:我编写了一个截断数据库 table 以进行清理的函数,但仍然希望获得有关如何改进我的测试的指示。
这不是单元测试,因为它有 side-effects。当然,您不能使用正在测试的 "delete" 函数。在通过单元测试之前,它甚至不存在。您的单元测试需要是 unit 测试。
您可能希望数据库连接被依赖注入,作为一个抽象数据存储。单元测试将使用模拟数据存储。
我想知道是否有人可以给我一些关于设计我的单元测试的指示,特别是被测试的功能出现在测试本身而不是出现在单元操作中的地方。
我正在用节点编写服务器端 javascript 并使用 rsvp 异步调用 restful 后端。
我的单元测试结构如下。
//Setup
//Action
//Assertion
//Cleanup
现在,我正在构建一个异步删除数据库中条目的函数,因此我的单元测试如下所示。
"deletingFunction_1entry_returns1EntryDeleted": function() {
return new rsvp.Promise(function( resolve, reject ) {
//Setup
var DatabaseEntry = { "Info": "Info" };
setup( DatabaseEntry ).then(function( xPostedEntries ) {
//if the setup promise resolves then...
//Action
xDelete( xPostedEntries ).then(function( xDeletedEnteries ) {
//Assertion
if ( xPostedEntries === xDeletedEntries ) {
//if the posted enteries matched the deleted enteries then...
resolve();
} else {
//otherwise the posted enteries did not match the deleted
//enteries so...
reject("The posted enteries did not match the deleted enteries.");
}
});
}, function( xPostedEnteries ) {
//otherwise the setup promise did not resolve so...
//Cleanup
/***************************************************************/
/* Here's where I'm having trouble. If the setup fails to post */
/* all of the entries to the database then the test is a */
/* scratch but I want to clean up all the posts that 'were' */
/* posted but to remove them from the database, I'd be using a */
/* deleting function, which is the function that I'm testing. */
/* So how can I redesign my unit test not to use the function */
/* that it's testing. */
/***************************************************************/
reject("The setup failed.");
});
});
}
已编辑:我编写了一个截断数据库 table 以进行清理的函数,但仍然希望获得有关如何改进我的测试的指示。
这不是单元测试,因为它有 side-effects。当然,您不能使用正在测试的 "delete" 函数。在通过单元测试之前,它甚至不存在。您的单元测试需要是 unit 测试。
您可能希望数据库连接被依赖注入,作为一个抽象数据存储。单元测试将使用模拟数据存储。