$ httpBackend.flush()需要调用每个测试:Jasmin-AngularJs单元测试

时间:2016-03-20 10:04:39

标签: angularjs unit-testing karma-jasmine httpbackend

不同,我的问题非常简单
 afterEach (function () {
        $httpBackend.verifyNoOutstandingExpectation ();
        $httpBackend.verifyNoOutstandingRequest ();
    });

为什么$httpBackend.flush()无法置于afterEach(function(){}内? 因为当我有几个测试用例时,每次我都需要调用它。

describe("test1", function(){
it('1', function(){
$httpBackend.flush()
})
it('2', function(){
$httpBackend.flush()
})
it('3', function(){
$httpBackend.flush()
})
})

即使我尝试在每个块之后放置$httpBackend.flush(),但我开始收到错误"错误:超时 - 在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超时内未调用异步回调。 "

$httpBackend.flush()

的每个函数之后,我只能有一个块可以自动执行吗?

由于

1 个答案:

答案 0 :(得分:1)

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; CMDeviceMotion *currentDeviceMotion = self.motionManager.deviceMotion; CMAttitude *currentAttitude = currentDeviceMotion.attitude; float roll = currentAttitude.roll; float pitch = currentAttitude.pitch; float yaw = currentAttitude.yaw; if (isLandscape) { _player.rotation = -yaw + currentLocation.x; // 0 roll is phone flat, so we need to rotate -90 degrees to adjust vertical phone if (orientation == UIDeviceOrientationLandscapeLeft) // Invert roll and add 180 degrees offset _player.pitch = (roll - RADIAN_90_DEGREES + RADIAN_180_DEGREES) + currentLocation.y; else _player.pitch = -(roll - RADIAN_90_DEGREES) + currentLocation.y; } else { if (orientation == UIDeviceOrientationFaceDown) _player.rotation = -yaw - currentLocation.x; else _player.rotation = -yaw + currentLocation.x; if (orientation == UIDeviceOrientationFaceUp) _player.pitch = (roll) + currentLocation.y; else if (orientation == UIDeviceOrientationFaceDown) _player.pitch = (roll) + currentLocation.y; else _player.pitch = -(roll) + currentLocation.y; } 应该在测试中调用,而不是测试之后调用。 它是测试的一部分,因此在测试后执行它是没有意义的。

$httpBackend.flush模拟将您的请求发送到服务器,之后您的可能flush方法将失败或通过。您还可以查看其他条件。

如果你想编写一个帮助方法来避免一直调用它,你可以编写类似这样的东西,例如:

httpBackend.expectXX