Merge pull request #35 from Microsoft/users/krishadi/deployment

Refactoring deployment test code to not mock task-lib
This commit is contained in:
Krishna Aditya 2016-01-27 12:35:47 +05:30
Родитель 172a00ee47 534225c4d9
Коммит b937a0c4cd
2 изменённых файлов: 32 добавлений и 18 удалений

Просмотреть файл

@ -16,12 +16,17 @@ export class MachineGroup {
export function saveMachineGroup(machineGroup: MachineGroup): void {
if (machineGroup == null) {
tl.error("Invalid machine group");
throwAndLog("Invalid machine group");
}
if (machineGroup.Name == null || machineGroup.Name.trim() == "") {
tl.error("Invalid machine group name");
throwAndLog("Invalid machine group name");
}
tl.setVariable(machineGroup.Name, JSON.stringify(machineGroup));
tl.debug("Saved machine group with name '" + machineGroup.Name + "'");
}
function throwAndLog(errorMessage: string) {
tl.error(errorMessage);
throw new Error(errorMessage);
}

Просмотреть файл

@ -11,22 +11,31 @@ import tl = require("vsts-task-lib/task");
var expect = chai.expect;
var sandbox;
var setVariableStub;
var logErrorStub;
function AssertLogAndError(call: Function, errorMessage: string): void {
logErrorStub.withArgs(errorMessage).throws(new Error(errorMessage));
describe("saveMachineGroup tests", (): void => {
var sandbox;
var logErrorSpy;
var setVariableSpy;
expect(call).to.throw(errorMessage);
logErrorStub.withArgs(errorMessage).should.have.been.calledOnce;
}
function AssertThrowsAndLogs(call: Function, errorMessage: string): void {
expect(call).to.throw(errorMessage);
logErrorSpy.withArgs(errorMessage).should.have.been.calledOnce;
}
describe("saveMachineGroup tets", (): void => {
beforeEach((): void => {
sandbox = sinon.sandbox.create();
setVariableStub = sandbox.stub(tl, "setVariable");
logErrorStub = sandbox.stub(tl, "error");
sandbox.stub(tl, "debug");
setVariableSpy = sandbox.spy(tl, "setVariable");
logErrorSpy = sandbox.spy(tl, "error");
// mock the std and err streams of vsts-task-lib to reduce noise in test output
var stdoutmock = {
write: function(message: string) {}
};
tl.setStdStream(stdoutmock);
tl.setErrStream(stdoutmock);
// vsts-task-lib calls process.exit in failure test cases
// we should eat that - test runner exits otherwise
sandbox.stub(process, "exit");
});
afterEach((): void => {
@ -34,12 +43,12 @@ describe("saveMachineGroup tets", (): void => {
});
it("should log and throw if machineGroup is null", (): void => {
AssertLogAndError(() => Deployment.saveMachineGroup(null), "Invalid machine group");
AssertThrowsAndLogs(() => Deployment.saveMachineGroup(null), "Invalid machine group");
});
it("should log and throw if name of the machine group is not set", (): void => {
var machineGroup = new MachineGroup();
AssertLogAndError(() => Deployment.saveMachineGroup(machineGroup), "Invalid machine group name");
AssertThrowsAndLogs(() => Deployment.saveMachineGroup(machineGroup), "Invalid machine group name");
});
it("should log and throw if name of the machine group is null or empty or whitepsace", (): void => {
@ -49,7 +58,7 @@ describe("saveMachineGroup tets", (): void => {
machineGroup.Name = invalidName;
sandbox.reset();
AssertLogAndError(() => Deployment.saveMachineGroup(machineGroup), "Invalid machine group name");
AssertThrowsAndLogs(() => Deployment.saveMachineGroup(machineGroup), "Invalid machine group name");
});
});
@ -72,6 +81,6 @@ describe("saveMachineGroup tets", (): void => {
Deployment.saveMachineGroup(machineGroup);
setVariableStub.withArgs(machineGroup.Name, JSON.stringify(machineGroup)).should.have.been.calledOnce;
setVariableSpy.withArgs(machineGroup.Name, JSON.stringify(machineGroup)).should.have.been.calledOnce;
});
});