0

http 呼び出しの応答をテストしようとしています。問題は、そこに入力した応答コードや応答データに関係なく合格することです。何か案は?ありがとう!

Controller.js

$scope.getPresses = function() {
    var pressRequest = {
        method: 'GET',
        url: localAPI.url + 'press/',
        headers: requestHeaders
    };
    $http(pressRequest)
        .success(function(data) {
            $scope.userPresses = data.results;
        })
        .error(function() {
            alert("We were not able to retrieve your press data");
        });
};

testController.js

    describe('Get Presses', function() {
        it("sets scope attributes for the user's presses", function() {
            $scope.getPresses()
            $httpBackend.expectGET('http://0.0.0.0:8000/api/press/', function() {
                return {
                    Authorization: "Token fakeToken2903920932"
                }
            }).respond(304, 'responseData')
            $httpBackend.flush()
        });
    });
4

1 に答える 1

1

まず、 のようなグローバル関数は避けてくださいalert。それらはテストが困難です。代わりに、$windowコントローラーに注入して使用します

$window.alert("We were not able to retrieve your press data");

テストに関しては、実際の結果をテストする必要があります。例えば...

var $window, $scope, $httpBackend;

beforeEach(function() {
    module('your.module', function($provide) {
        $provide.value('$window', $window = jasmine.createSpyObj('$window', ['alert']));
    });

    // and however else you set up your $scope and $httpBackend vars
});

it('assigns results to userPresses on success', function() {
    $httpBackend.expectGET('http://0.0.0.0:8000/api/press/', function(headers) {
        return headers['Authorization'] === 'Token fakeToken2903920932';
    }).respond({results: 'results'});

    $scope.getPresses();
    $httpBackend.flush();

    expect($scope.userPresses).toBe('results');
});

it('calls alert on error', function() {
    $httpBackend.whenGet('http://0.0.0.0:8000/api/press/').respond(500);
    $scope.getPresses();
    $httpBackend.flush();

    expect($window.alert).toHaveBeenCalled();
});
于 2015-02-12T23:15:25.337 に答える