Angularjs 角度:单元测试if-else语句
我正在尝试编写一个简单的测试,如:Angularjs 角度:单元测试if-else语句,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我正在尝试编写一个简单的测试,如: it('should return smaller', function() { expect($scope.test).toEqual('smaller'); }); it('should return bigger', function() { $scope.x = 15; expect($scope.test).toEqual('bigger'); }); 控制器: $scope.x = 4; if($scope.x >
it('should return smaller', function() {
expect($scope.test).toEqual('smaller');
});
it('should return bigger', function() {
$scope.x = 15;
expect($scope.test).toEqual('bigger');
});
控制器:
$scope.x = 4;
if($scope.x > 6) {
$scope.test = 'bigger';
}else{
$scope.test = 'smaller';
}
第一次测试正确,但第二次测试失败。在测试中设置$scope.x=15不会改变这种情况。
那我该怎么测试呢
Live:控制器中的逻辑包含哪些功能?或者它只是在构造函数中?如果它在控制器的构造函数中,那么在测试中,您需要控制该逻辑何时运行(即,当创建控制器时。您应该将控制器中的测试代码放入函数中,并在运行时调用它,然后在为
$scope.x
@PierreMaoui指定其他值后再次调用它。确实如此!谢谢!