Checking for the presence of a filter in Angular

I am new to unit testing and trying to figure things out. I am working hard to follow this guide: http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-testacular.html#testing-filters . In my AngularJS application, I have a filter that I need to check. I have Node, Testacular and Jasmine configured and working correctly. The filter I'm trying to check is pretty simple:

myApp.filter('bill_ship', function () {
    return function (userData) {
        var output = "---";
        switch (userData) {
            case "0":
                output = "Billing";
                break;
            case "1":
                output = "Shipping";
                break;
        }
        return output;
    }
});

I thought I set up my test correctly, but it constantly fails.

describe("Unit Testing: Filters - ", function() {
    beforeEach(angular.mock.module('prismApp', ['bill_ship']));

    //BillShip Filter
    it('should have a bill-ship filter: ', inject(function($filter){
        expect($filter('bill_ship')).not.toEqual(null);
    }));
});

This message failed: Error: argument 'fn' is not a function, received a string from bill_ship.

So ... where am I doing this wrong?

+5
source share
2

, ...

LOT, , . , .

+1

- ( , ), , @MBielski , ! , , unit test, .

:

angular.module('prismApp.filters', [])

  .filter('billShip', function () {
    return function (userData) {
      // define filter
      ...
    }
  });

:

describe("Unit Testing: Filters - ", function() {
  var billShip;

  // load the module
  beforeEach(module('prismApp.filters'));

  // load filter function into variable
  beforeEach(inject(function ($filter) {
    billShip = $filter('billShip');
  }));

  // test billShip filter
  it('should have a bill-ship filter: ', function () {
    // test away!
    expect(billShip).not.toEqual(null);
    expect(billShip("0")).toEqual("Billing");
    ...
  });
});

, .

+24

All Articles