How to mock angular.module('myModule', []).value() in Jasmine/Protractor

后端 未结 1 662
借酒劲吻你
借酒劲吻你 2021-01-05 23:54

I\'m trying to use Protractor\'s addMockModule() to mock a simple AngularJS module and override a variable:

Here is my HTML:



        
相关标签:
1条回答
  • 2021-01-06 00:25

    According to Juliemr in this post:

    https://github.com/angular/protractor/issues/509

    " the code that is run in the mockedModule is run in a separate context from the test. One is in the browser, one is in the node.js process running the test "

    Put declaration of mockedValue inside it() and enable angular synchronization

    it('should override services via mock modules', function () {
        ptor = protractor.getInstance();
    
        // When ignoreSynchronization is true mocking doesn't work
        ptor.ignoreSynchronization = false; 
    
        browser.addMockModule('myModule', function () {
            var module = angular.module('myModule').config(['$provide', function ($provide) {
                $provide.constant('myValue', 'newMockedValue');
            }])
        });
    
        browser.get('http://localhost:57627/page1.html');
        expect(element(by.binding("myValue")).getText()).toBe("newMockedValue");
    
    });
    
    0 讨论(0)
提交回复
热议问题