5

I have the following unit tests, and for some reason the second test makes other tests fail.

beforeEach(inject(function ($rootScope, _$httpBackend_, $controller, $location, mockedResource) {
    scope = $rootScope.$new();
    httpBackend = _$httpBackend_;
    locationService = $location;

    ctrlDependencies = {
        $scope: scope, 
        resource: mockedResource,
    }

    var ctrl = $controller('myController', ctrlDependencies);
}));

it('should redirect to a new page', function() {
    scope.pageRedirectFunction();
    expect(locationService.path()).toBe('/newpage')
});

it('should delete an epic resource', function() {
    httpBackend.expectGET('/api/v1/epic/1').respond({});
    httpBackend.expectDELETE('/api/v1/epic/1').respond({});

    // Run the deletion function
    scope.deleteEpicResource()

    httpBackend.flush() // This line seems to be the rebelious one

    expect(scope.epicResources.length).toEqual(0)
})

I have managed to figure out the line that seems to cause the errors, and it's the httpBackend.flush() line. Why is the flush function causing strange behaviour?

The actual error I get from running the command karma start in the terminal, is:

 Delaying execution, these browsers are not ready: Chrome 29.0 ....

after a little while, the Chrome session then crashes.

4

2 に答える 2

2

jasmine と AngularJS を使用した非同期リクエストのテスト/モックに関するあまり知られていないヒント:

テストでリクエストを明示的に呼び出していない場合 (つまり、別の関数を介して呼び出している場合)、リクエストは Angular によって消化されないため、( を呼び出したときにflush())リクエストが起動されていないように見えます。

呼び出しのscope.$digest()前に実行してみてください。うまくいくかもしれません。httpBackend.flush()詳細については、このスレッドを参照してください。

于 2013-09-04T17:07:06.723 に答える