programing

재스민 스파이의 반품가치는 어떻게 변경하나요?

kingscode 2023. 3. 12. 12:21
반응형

재스민 스파이의 반품가치는 어떻게 변경하나요?

난 재스민을 이용해서 스파이를 만들고 있어

beforeEach(inject(function ($injector) {
    $rootScope = $injector.get('$rootScope');
    $state = $injector.get('$state');
    $controller = $injector.get('$controller');

    socket = new sockMock($rootScope);

    //this is the line of interest
    authService = jasmine.createSpyObj('authService', ['login', 'logout', 'currentUser']);
}));

다양한 방법으로 반환되는 것을 변경할 수 있으면 좋겠습니다.authService.

실제 테스트는 다음과 같이 설정됩니다.

function createController() {
    return $controller('UserMatchingController', {'$scope': $rootScope, 'socket':socket, 'authService': authService });
}

describe('on initialization', function(){
    it('socket should emit a match', function() {
        createController();

        expect(socket.emits['match'].length).toBe(1);
    });

    it('should transition to users.matched upon receiving matched', function(){

        //this line fails with "TypeError: undefined is not a function"
        authService.currentUser.andReturn('bob');

        createController();

        $state.expectTransitionTo('users.matched');
        socket.receive('matchedblah', {name: 'name'});

        expect(authService.currentUser).toHaveBeenCalled()
    })
})

컨트롤러의 설정 방법은 다음과 같습니다.

lunchrControllers.controller('UserMatchingController', ['$state', 'socket', 'authService',
    function ($state, socket, authService) {
        socket.emit('match', {user: authService.currentUser()});

        socket.on('matched' + authService.currentUser(), function (data) {
            $state.go('users.matched', {name: data.name})
        });
    }]);

기본적으로 스파이 메서드의 반환값을 변경할 수 있으면 좋겠습니다.하지만, 제가 이 문제에 올바르게 접근하고 있는지는 잘 모르겠습니다.jasmine.createSpyObj.

대신 이거 먹어봐.Jasmine 2.0에서는 API가 변경되었습니다.

authService.currentUser.and.returnValue('bob');

문서:

https://jasmine.github.io/tutorials/spying_on_properties

이렇게 덮어쓸 수 있어요

updateService.getUpdate = jasmine.createSpy().and.returnValue('bob')

언급URL : https://stackoverflow.com/questions/28890424/how-to-change-return-value-of-jasmine-spy

반응형