我有一个SNumber类
export class SNumber extends Symbol{
private _value: number;
constructor(_value: number){
super(SymbolType.VARIABLE);
this._value = _value;
}
public add(symbol: Symbol, symbolic?: boolean): Symbol {
if(symbol instanceof SNumber && !symbolic){
return new SNumber(this._value + symbol._value);
}
return new SExpression(Arrays.toList(this.copy(), symbol));
}
public substract(symbol: Symbol, symbolic?: boolean): Symbol {
if(symbol instanceof SNumber && !symbolic){
this.add(symbol.negate(), false);
}
return new SExpression(Arrays.toList(this.copy(), symbol.negate()));
}
public multiply(symbol: Symbol, symbolic?: boolean): Symbol {
if(symbol instanceof SNumber && !symbolic){
return new SNumber(this._value*symbol._value);
}
return new SMultiplication(this.copy(), symbol);
}
public divide(symbol: Symbol, symbolic?: boolean): Symbol {
if(symbol instanceof SNumber && !symbolic){
if(symbol._value == 0){
throw Error("Division by null");
}
return new SNumber(this._value / symbol._value);
}
return new SFraction(this.copy(), symbol);
}
public negate(symbolic?: boolean): Symbol {
return new SNumber(-1*this._value);
}
public invert(symbolic?: boolean): Symbol {
if(!symbolic){
return new SNumber(1/this._value);
}
return new SFraction(Constants.ONE, this.copy());
}
public copy(): Symbol{
return new SNumber(this._value);
}
public get value(): number{
return this._value;
}
}
我正在尝试通过简单的茉莉花测试来测试其方法。
import { SNumber } from './SNumber'; describe('Testing aritmetic methods ', () => { let number1:SNumber; let number2:SNumber; beforeEach(()=>{ number1 = new SNumber(2); number2 = new SNumber(4); }) it('When adding number1 to number2, should return 6', () => { var result = number1.add(number2); expect(result).toEqual(6); }); it('When substracting number1 from number2, should return 2', () => { var result = number1.substract(number2); expect(result).toEqual(2) }); });
但是由于某些原因,如果我的测试包含关键字new,那么它将不起作用。 业力不会写任何错误,只是在过程的一半停止编译。 实例化(新)是否在beforeEach函数或其他任何地方都没有关系。 如果我像这样写更简单的测试
describe('Testing aritmetic methods ', () => { let number1 let number2 beforeEach(()=>{ number1 = 2; number2 = 4; }) it('When adding number1 to number2, should return 6', () => { var result = number1 + number2; expect(result).toEqual(6); }); it('When substracting number1 from number2, should return 2', () => { var result = number1 - number2; expect(result).toEqual(2) });
一切正常。
谢谢您的建议。