1

モックが返すものを整理できるように、jasmine でコンテキストを使用したいと考えています。これは、私がやりたいことを示すための疑似コードです。私は、これらの期待の両方が合格することを期待しています:

describe('a module', function(){
    var whatTheFunctionReturns;
    beforeEach(function(){
        module('anApp', function($provide){
            $provide.value('aFactory', { aFunction: whatTheFunctionReturns })
        }
    });

    describe('when the function returns alpha', function(){
        whatTheFunctionReturns = 'alpha'

        it('should get data from a service', function(){
            expect(aFactory.aFunction).toEqual( 'alpha' )
        }); 
    });
    describe('when the function returns beta', function(){
        whatTheFunctionReturns = 'beta'

        it('should get data from a service', function(){
            expect(aFactory.aFunction).toEqual( 'beta' )
        }); 
    });
});

上記をよくお読みください。私が何をしようとしているのか分かりますか?コード

$provide.value('aFactory', { aFunction: whatTheFunctionReturns })

は beforeEach ブロックに 1 回書き込まれますが、変数は

whatTheFunctionReturns

は 2 つの記述ブロックで変更され、when the function returns alphawhen the function returns beta.

しかし、うまくいきません。これは、コントローラーをテストし、それが依存するファクトリーをモックアウトしようとしている実際のコードです。

describe('firstController', function(){
    var $rootScope, $scope, $controller
    var message = 'I am message default'

    beforeEach(function(){
        module('App',function($provide){
            $provide.value('ServiceData', { message: message})
        });
        inject(function(_$rootScope_,_$controller_){
            $rootScope = _$rootScope_
            $scope = $rootScope.$new()
            $controller = _$controller_
            $controller('firstController', { '$rootScope' : $rootScope, '$scope' : $scope })
        });
    });

    describe('when message 1', function(){
        beforeEach(function(){
            message = 'I am message one'
        });
        it('should get data from a service', function(){
            expect($scope.serviceData.message).toEqual( '1' ) // using wrong data so I can see what data is being returned in the error message
        }); 
    });

    describe('when message 2', function(){
        beforeEach(function(){
            message = 'I am message two'
        });
        it('should get data from a service', function(){
            expect($scope.serviceData.message).toEqual( '2' ) // using wrong data so I can see what data is being returned in the error message
        });
    });
});

返されるエラーメッセージは次のとおりです。

Firefox 34.0.0 (Ubuntu) firstController when message 1 should get data from a service FAILED
    Expected 'I am message default' to equal '1'.

Firefox 34.0.0 (Ubuntu) firstController when message 2 should get data from a service FAILED
    Expected 'I am message one' to equal '2'.

半分稼働中です。変数は更新されていますが、最後の記述ブロック ( ) でのみ更新されてい'when message 2'ます。返されると予想していたものは次のとおりです。

Firefox 34.0.0 (Ubuntu) firstController when message 1 should get data from a service FAILED
    Expected 'I am message one' to equal '1'.

Firefox 34.0.0 (Ubuntu) firstController when message 2 should get data from a service FAILED
    Expected 'I am message two' to equal '2'.

どうすればこれを達成できますか?私が記述ブロックで何をしようとしているのか分かりますか?

4

3 に答える 3