How do I unit test $scope.broadcast, $scope.$on using Jasmine

前端 未结 1 1694
生来不讨喜
生来不讨喜 2021-01-25 04:49

I\'m newbie to AngularJs/NodeJs world, so forgive if this is a basic question to some.

So in a nutshell I\'ve two controllers, the first controller $broadcast

相关标签:
1条回答
  • 2021-01-25 05:29

    First you should do the broadcast on $rootScope then you can receive on $scope. Now to the testing. I assume you want to include real request to your API via bookService and $http. This can be mocked but I'll focus on the real call. Let me know if you need the mocked one.

    Before the actual test, you will need to do some injections/instantiations:

    • Initialize your app
    • Inject $controller, $rootScope, $httpBackend and bookService
    • Create scopes for firstController and SecondController and store it in a variable
    • Store bookService and $httpBackend in variables
    • Instantiate the controllers and store them

    Then in the actual test you must tell $httpBackend what to do when it caches request for the books (or books). Construct $httpBackend.whenGET("/api/books/1").passThrough(); will pass request with url "/api/books/1" to the server. Next your must setup property selectedRows on firstScope so it fulfills the condition in function selectGridRow in your firstCtrl.

    Now you can call function selectGridRow to trigger the broadcast and API call. But you must wrap it in runs function so Jasmine recognizes this as an async call and will wait for it to finish. The 'waiting' is defined in waitsFor call. It will wait until it gets a book and it waits max 5000 ms then the test will be marked as failed.

    Last step is to check expected result. We don't have to check for undefined anymore as the test would not get to here anyway. The check must be wrapped again runs call so it is executed afters successful 'waitsFor'.

    Here is the full code:

    describe("Broadcast between controllers", function () {
    
        beforeEach(module('app')); //app initialization
    
        var firstScope;
        var secondScope;
    
        var bookService;
        var $httpBackend;
    
        var firstController;
        var secondController;
    
        beforeEach(inject(function ($controller, $rootScope, _bookService_, _$httpBackend_) {
            firstScope = $rootScope.$new();
            secondScope = $rootScope.$new();
    
            bookService = _bookService_;
            $httpBackend = _$httpBackend_;
    
            firstController = $controller('firstCtrl', { $scope: firstScope });
            secondController = $controller('secondCtrl', { $scope: firstScope, bookService: bookService });
        }));
    
    
        it("should work", function () {
    
            $httpBackend.whenGET("/api/books/1").passThrough();
    
            firstScope.selectedRows = [{ id: 1, total: 1000 }];
            secondScope.book = null;
    
            runs(function () {
                firstScope.selectGridRow();
            });
    
            waitsFor(function () {
                return secondScope.book != null;
            }, "Data not received in expected time", 5000);
    
            runs(function () {
                expect(secondScope.book[0].id).toEqual(1);
            });
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题