在分配给我的一个相当新的项目中,已经编写了一些代码和测试用例,一些现有的测试用例突然开始意外失败,并且存根没有正确恢复。
测试套件的beforeEach和afterEach挂钩如下:
var getTokenStub, paramDescStub, getDataStub, getCaseDesiredAction;
beforeEach(function () {
getCaseDesiredAction = sinon.stub(pgUtils, 'getCaseDesiredAction');
getDataStub = sinon.stub(apiCall, 'getData');
getTokenStub = sinon.stub(apiCall, 'getToken');
paramDescStub = sinon.stub(paramDesc, 'getParameterDescriptionForCode')
});
afterEach(function () {
pgUtils.getCaseDesiredAction.restore();
apiCall.getData.restore();
apiCall.getToken.restore();
paramDesc.getParameterDescriptionForCode.restore();
});
....
接下来是一些检查错误的测试用例。
这应该通过错误回复
....
it('should respond with error if query fails', (function (done) {
var req = {
query: {caseId: '12345'},
};
getTokenStub.yields(200, '"Token"');
getDataStub.yields(200, sampleDetailsResponse);
getCaseDesiredAction.yields('error', null);
getCaseDetails(req, res);
res.response.errorCode.should.equal(1234);
res.response.msg.should.equal('caseDetailsResult Failed while getting desired action: error');
done();
}));
....
问题在于,即使是下一个测试用例也会出现与之前测试用例相同的错误。例如: - 即使所有输入都正确,下一个案例也会因错误'caseDetailsResult Failed while getting desired action: error'
而失败。如果我注释掉检查错误输入/状态的测试用例,则下一个案例正常工作。
这应该通过成功响应(但它失败并出现与之前测试案例相同的错误)
it('should respond with case details', (function (done) {
var req = {
query: {caseId: '12345'},
session: {
"authenticatedUser": {userId: "999999999"}
}
};
getTokenStub.yields(200, '"Token"');
getCaseDesiredAction.yields(null, [{desiredaction: 'Close'}]);
paramDescStub.yields('TEST CODE');
getDataStub.yields(200, sampleDetailsResponse);
getCaseDetails(req, res);
res.response.should.eql({
caseDetails:
{
....
}
success: 'true'
});
done();
}));
我在海上试图找到这里出了什么问题。还有其他文件遵循相同的格式,但它们没有失败。有人可以帮我解决这个问题吗?
注意:这里要注意的一件事是,最近我们在原始文件中添加了一个async.map函数。
更新:以下是规范的完整代码段
describe('getCaseDetails', function () {
var res = {
response: {},
send: function (values) {
this.response = values;
}
};
var getCaseDesiredAction;
var getDataStub;
var getTokenStub, paramDescStub;
beforeEach(function () {
getCaseDesiredAction = sinon.stub(pgUtils, 'getCaseDesiredAction');
getDataStub = sinon.stub(apiCall, 'getData');
getTokenStub = sinon.stub(apiCall, 'getToken');
paramDescStub = sinon.stub(paramDesc, 'getParameterDescriptionForCode')
});
afterEach(function () {
pgUtils.getCaseDesiredAction.restore();
apiCall.getData.restore();
apiCall.getToken.restore();
paramDesc.getParameterDescriptionForCode.restore();
});
// passes with error response invalid input
it('should return an error if an input is missing', function (done) {
var req = {
query: {},
session: {}
};
getCaseDetails(req, res);
res.response.msg.should.equal("Invalid input");
done();
});
....
....
// passes with error response 'Failed while getting desired action'
it('should respond with error if query fails', (function (done) {
var req = {
query: {caseId: '12345'},
};
getTokenStub.yields(200, '"Token"');
getDataStub.yields(200, sampleDetailsResponse);
getCaseDesiredAction.yields('error', null);
getCaseDetails(req, res);
res.response.errorCode.should.equal(1234);
res.response.msg.should.equal('caseDetailsResult Failed while getting desired action: error');
done();
}));
....
....
it('should respond with case details', (function (done) {
var req = {
query: {caseId: '12345'},
session: {
"authenticatedUser": {userId: "999999999"}
}
};
getTokenStub.yields(200, '"Token"');
getCaseDesiredAction.yields(null, [{desiredaction: 'Close'}]);
paramDescStub.yields('TEST CODE');
getDataStub.yields(200, sampleDetailsResponse);
getCaseDetails(req, res);
res.response.should.eql({
caseDetails:
{
....
}
success: 'true'
});
done();
}));
});
答案 0 :(得分:0)
您的res
对象正在从一个测试进入另一个测试状态。您应该在beforeEach
中而不是直接在describe块中初始化它。