我的单元测试是:
describe.only("Validator Service Tests", function () {
let request
before((done) => {
request = sinon.stub()
done()
})
beforeEach(() => {
process.env.API_URL = "http://test"
})
it('Should return with no errors if the file matches the schema', () => {
const updateStatusSpy = sinon.spy(FileLib, 'updateStatus')
request.yields({message: 'ok'})
return ValidatorService.handleMessage({
file: 'test'
})
.then((response) => {
assert()
console.log(response)
sinon.assert.calledOnce(updateStatusSpy)
assert(response, 'f')
})
})
})
问题是我的handleMessage
功能,如下所示:
exports.handleMessage = (message, done) => {
return stuff()
.then((result) => {
console.log('result', result)
if(result) {
return FileLib.updateStatus(fileId, 'valid')
}
return FileLib.updateStatus(fileId, 'invalid')
})
.then(done)
}
我的updateStatus
功能:
exports.updateStatus = function(fileId, status) {
console.log(fileId, status)
return request.put({
uri: `${process.env.API_URL}/stuff/${fileId}`,
body: {
status: status
}
})
}
我的实际request
电话被埋得太深了,我怎样才能在测试时将其删除?
答案 0 :(得分:1)
我不确定我是否完全理解你的问题,但如果你只是试图存根,请尝试这样的事情:
public class ProcessService {
private List<GeneralProcess> processList;
private int i;
public ProcessService() {
processList=new Repository().list(); //fetch all data from DB
}
public List<GeneralProcess> findAll(int offset, int limit, String
filter){
Collections.sort(processList,Comparator
.comparing(GeneralProcess::getName));
if(filter!=null) {
return processList.stream().filter((s) ->
s.getName().startsWith(filter))
.collect(Collectors.toList()).subList(offset,offset+limit);
}
else {
return processList.subList(offset,offset+limit);
}
}
public int count() {
return processList.size();
}