AngularJS and Jasmine testing factory

steveareeno

I am new to angularjs unit testing. I have a factory I am trying to spy on with jasmine and I can't figure out the syntax for the test spec. Below is the factory:

app.factory('assetFactory', function ($http) {
    var baseAddress = "../api/";
    var url = "";
    var factory = {};

    factory.getAssets = function (term) {
        url = baseAddress + "asset/search/" + term;
        return $http.get(url);
    };
    return factory;
});

Here is my test spec, which fails on the expect statement (Error: Expected spy getAssets to have been called):

describe('assetFactory', function () {
    beforeEach(function () {
        module('fixedAssetApp');
    });

    beforeEach(inject(function (assetFactory) {
        spyOn(assetFactory, 'getAssets').and.callThrough();
    }));

    it('should be defined', inject(function (assetFactory) {
        expect(assetFactory).toBeDefined();
    }));

    it('should have been called, inject(function (assetFactory) {
        expect(assetFactory.getAssets).toHaveBeenCalled();

    }));
});
RIYAJ KHAN

Please add this change.

beforeEach(inject(function (assetFactory) {
        spyOn(assetFactory, 'getAssets').and.callThrough();
        assetFactory.getAssets();
    }));

In order to toHaveBeenCalled() return true, you must called your function either in beforeEach or it block.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Testing an AngularJS factory with Karma (Jasmine)

From Dev

angularJS factory testing using a stand-alone jasmine server only

From Dev

Testing AngularJS resource with Jasmine

From Dev

Unit testing Angularjs jasmine

From Dev

Angularjs and jasmine, testing promise

From Dev

AngularJS jasmine factory tests with spy

From Dev

AngularJS jasmine factory tests with spy

From Dev

Testing AngularJS promises in Jasmine 2.0

From Dev

Integration Testing AngularJS + Karma + Jasmine

From Dev

AngularJS testing : Jasmine mock callback

From Dev

Testing parameterized AngularJS filter in Jasmine

From Dev

AngularJS Jasmine testing init functions

From Dev

expect() in .then() - jasmine unit testing, AngularJS

From Dev

AngularJS Jasmine testing init functions

From Dev

Testing nested promise in Jasmine using mocked factory

From Dev

AngularJS Jasmine spy provided factory and callthrough

From Dev

AngularJS Jasmine testing get-request

From Dev

Angularjs and Jasmine : testing with multiple instances of the same service

From Dev

Unit-Testing a filter with Jasmine in AngularJS

From Dev

AngularJS Jasmine testing google maps: offsetWidth is null

From Dev

CSS issue testing AngularJS directives with Karma + Jasmine

From Dev

AngularJS Testing: Protractor, Karma, Jasmine in a Yeoman App

From Dev

mock angularjs service for unit testing using jasmine

From Dev

angularjs 1 and jasmine, service promise testing

From Dev

Testing AngularJS controller with Jasmine causes error in RubyMine

From Dev

AngularJS Jasmine 2.0 async testing timed out

From Dev

AngularJS promise not calling then when testing with Jasmine

From Dev

AngularJS jasmine promise testing fails because of timeout

From Dev

Angularjs and Jasmine : testing with multiple instances of the same service

Related Related

  1. 1

    Testing an AngularJS factory with Karma (Jasmine)

  2. 2

    angularJS factory testing using a stand-alone jasmine server only

  3. 3

    Testing AngularJS resource with Jasmine

  4. 4

    Unit testing Angularjs jasmine

  5. 5

    Angularjs and jasmine, testing promise

  6. 6

    AngularJS jasmine factory tests with spy

  7. 7

    AngularJS jasmine factory tests with spy

  8. 8

    Testing AngularJS promises in Jasmine 2.0

  9. 9

    Integration Testing AngularJS + Karma + Jasmine

  10. 10

    AngularJS testing : Jasmine mock callback

  11. 11

    Testing parameterized AngularJS filter in Jasmine

  12. 12

    AngularJS Jasmine testing init functions

  13. 13

    expect() in .then() - jasmine unit testing, AngularJS

  14. 14

    AngularJS Jasmine testing init functions

  15. 15

    Testing nested promise in Jasmine using mocked factory

  16. 16

    AngularJS Jasmine spy provided factory and callthrough

  17. 17

    AngularJS Jasmine testing get-request

  18. 18

    Angularjs and Jasmine : testing with multiple instances of the same service

  19. 19

    Unit-Testing a filter with Jasmine in AngularJS

  20. 20

    AngularJS Jasmine testing google maps: offsetWidth is null

  21. 21

    CSS issue testing AngularJS directives with Karma + Jasmine

  22. 22

    AngularJS Testing: Protractor, Karma, Jasmine in a Yeoman App

  23. 23

    mock angularjs service for unit testing using jasmine

  24. 24

    angularjs 1 and jasmine, service promise testing

  25. 25

    Testing AngularJS controller with Jasmine causes error in RubyMine

  26. 26

    AngularJS Jasmine 2.0 async testing timed out

  27. 27

    AngularJS promise not calling then when testing with Jasmine

  28. 28

    AngularJS jasmine promise testing fails because of timeout

  29. 29

    Angularjs and Jasmine : testing with multiple instances of the same service

HotTag

Archive