我有一些方法的对象:
module.exports = {
auth: async function() {
// Load the key
const key = JSON.parse(fs.readFileSync("./keys/key.json").toString());
// Auth using the key
var auth;
try {
auth = await google.auth.fromJSON(key);
} catch (e) {
console.error(e);
}
// Add read / write spreadsheets scope to our auth client
auth.scopes = ["https://www.googleapis.com/auth/spreadsheets"];
// Create an instance of sheets to a scoped variable
try {
this.sheets = await google.sheets({ version: "v4", auth });
} catch (e) {
console.error(e);
}
},
writeSingleRow: async function(spreadsheetId, sheetName, values, index) {
// Create the resource for google sheets
const resource = {
values
};
// Write out to the spreadsheet
const res = await this.sheets.spreadsheets.values.update({
spreadsheetId: spreadsheetId,
range: `${sheetName}!A${index + 2}:D${values.length + 1 + index}`,
valueInputOption: "RAW",
resource: resource
});
//console.log("Updated spreadsheet!");
},
readSingleRow: async function(spreadsheetId, sheetName, row) {
const range = sheetName + "!" + "A" + row + ":" + "AU" + row;
try {
const response = (
await this.sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: range,
majorDimension: "ROWS",
valueRenderOption: "UNFORMATTED_VALUE", // TODO: Update placeholder value.
dateTimeRenderOption: "FORMATTED_STRING" // TODO: Update placeholder value.
})
).data;
// TODO: Change code below to process the `response` object:
return response;
} catch (err) {
console.error(err);
}
}
};
而且我正在尝试测试每个。问题在于读取和写入方法需要将参数发送给它们,并且似乎没有一种方法可以做到这一点。
这是我尝试过的:
//readSingleRow
describe("readSingleRow", () => {
it("should not throw and return an object with \'values\' property", (done) => {
expect(sheets.readSingleRow(spreadsheetId,sheetName,row)).to.not.throw();
done();
});
});
和
describe("readSingleRow", () => {
it("should not throw and return an object with \'values\' property", (done) => {
var values = sheets.readSingleRow(spreadsheetId,sheetName,row);
expect(values).to.be.an(object);
done();
});
});
没有任何作用。如何将参数传递给此函数?
或者,如果我不在这里,我应该怎么做?我当然应该能够测试带有参数的函数的调用。除非我缺少一些重要的测试功能,否则看起来很简单。
已解决:
结果我犯了两个错误:
这是错误的数据类型(我之前检查过返回的值,它是console.logging一个对象)
describe("readSingleRow", function() {
it("should not throw and return an object with \'values\' property", async function(){
await sheets.auth();
const values = await sheets.readSingleRow(spreadsheetId,sheetName,row);
expect(values.values).to.be.array();
});
})