Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Filter Presence in Angular

I'm new to unit testing and trying to grasp things. I'm doing my best to follow this tutorial: http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-testacular.html#testing-filters. In my AngularJS app I have a filter that I need to test. I have Node, Testacular, and Jasmine set up and running properly. The filter I am trying to test 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 had my test set up correctly, but it consistently 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);
    }));
});

It fails with this message: Error: Argument 'fn' is not a function, got string from bill_ship.

So... where am I doing this wrong?

like image 372
MBielski Avatar asked Feb 20 '13 19:02

MBielski


People also ask

How do you filter a test?

Since filtration is the process of removing particles from a fluid, particle counters are a tool frequently used to test the functioning of the filter. The feed (original fluid) and the filtrate (fluid that passes through the filter) are measured with a liquid particle counter such as our AccuSizer SPOS system.

Why would you use a spy in a test in Angular?

A Spy is a feature of Jasmine which lets you take an existing class, function, or object and mock it in such a way that you can control what gets returned from function calls. We create a real instance of AuthService and inject it into the LoginComponent .

What is beforeEach in Angular testing?

beforeEach is a global function in Jasmine that runs some setup code before each spec in the test suite. In this test suite, beforeEach is used to create a testing module using the TestBed object and declares any components that would be used in this testing module.


2 Answers

If anyone else is wondering (I spend some time before I found out) I thought that @MBielski's way of injecting filters was a little confusing, but deffinately does the job! However, I found this to be a good way to inject filters into jasmine unit test environment to test them.

Here's the filter definition:

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

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

And the test:

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");
    ...
  });
});

Hope this helps anyone else.

like image 79
mlunoe Avatar answered Nov 08 '22 23:11

mlunoe


Ok, this why I should never code without caffine...

It helps a LOT when you put the files that you want to test in the config file. Boy do I feel stupid.

like image 20
MBielski Avatar answered Nov 09 '22 00:11

MBielski