我正在尝试测试getDatabase版本函数是否会将查询传递给getDatabase调用的getQueryResult()中的executeQuery函数。
代码如下:
`
var RequestHandler = function () {
this.getQueryResult = function (sQuery) {
this.checkConnection();
try {
return this.conn.executeQuery.apply(this.conn, arguments);
} catch (err) {
this.conn.close();
this.conn = null;
throw "unexpected error, please check application trace files for more information";
}
};
this.getDatabaseVersion = function() {
var query = "select top 1 VERSION from M_DATABASE";
return this.getQueryResult(query)[0].VERSION;
};
}
`
我写的测试用例:
`
var RHandler = $.import("sap.hana.graph.viewer.XS_Backend.js.lib", "RequestHandler").RequestHandler;
describe ("getDatabaseVersion Tests", function (){
var rHandler = null;
var getQueryResult = jasmine.createSpyObj('getQueryResult', ['conn', 'executeQuery', 'close']);
var conn = jasmine.createSpyObj('conn', ['executeQuery', 'close']);
beforeEach(function() {
rHandler = new RHandler();
rHandler.openConnection();
});
function getAllQueries() {
return getQueryResult.conn.executeQuery.calls.allArgs().join(':::');
}
it('should return the databaseVersion and match the query sent to getQueryResult', function() {
rHandler.getDatabaseVersion();
expect(rHandler.getDatabaseVersion()).toEqual("2.00.030.00.1502184660");
expect(getAllQueries()).toEqual(/select top 1 VERSION from M_DATABASE/i);
});
});
`
但这根本不起作用。我走错路了。谁能指导我或让我知道我做错了什么。
最佳答案
您需要这样的东西:
var RHandler = $.import("sap.hana.graph.viewer.XS_Backend.js.lib", "RequestHandler").RequestHandler;
describe('RequestHandler', () => {
let rHandler = null;
let connection = null
beforeEach(() => {
connection = jasmine.createSpyObj('conn', ['executeQuery', 'close'])
rHandler = new RHandler();
rHandler.conn = connection;
});
describe('getQueryResult', () => {
beforeEach(() => {
spyOn(rHandler, 'checkConnection');
});
it('should check connection', () => {
rHandler.getQueryResult();
expect(rHandler.checkConnection).toHaveBeenCalled();
});
it('should execute query', () => {
connection.executeQuery.and.returnValue('foo');
const actual = rHandler.getQueryResult('bar', 'baz');
// toHaveBeenCalledWithContext
// https://www.npmjs.com/package/jasmine-spy-matchers
expect(connection.executeQuery).toHaveBeenCalledWithContext(connection, 'bar', 'baz');
// or default toHaveBeenCalledWith
expect(connection.executeQuery).toHaveBeenCalledWith('bar', 'baz');
expect(actual).toBe('foo');
});
it('should throw error', () => {
connection.executeQuery.and.throwError(new Error('some error message'));
expect(() => {
rHandler.getQueryResult('bar', 'baz');
}).toThrow('unexpected error, please check application trace files for more information');
expect(rHandler.conn).toBe(null);
});
});
describe('getDatabaseVersion', () => {
it('should return version', () => {
spyOn(rHandler, 'getQueryResult').and.returnValue([{VERSION: '2.00.030.00.1502184660'}])
const actual = rHandler.getDatabaseVersion();
expect(rHandler.getQueryResult).toHaveBeenCalledWith('select top 1 VERSION from M_DATABASE')
expect(actual).toBe('2.00.030.00.1502184660');
});
});
});