Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a working test for the factory as you posted it. I added a variable $httpBackend for the injected httpBackend. And a call to $httpBackend.flush(). <kbd><a href="http://jsfiddle.net/klode/zVUL2/" rel="nofollow"><strong>fiddle-demo</strong></a></kbd> (read to the end for full description of fiddle content)</p> <pre><code>describe( 'Global Service: ', function() { var Global, $httpBackend // load the relevant application module, with the service to be tested beforeEach( module('bkJoga') ); beforeEach( function() { // inject the mock for the http backend inject(function(_$httpBackend_) { $httpBackend = _$httpBackend_; }); // mock the response to a particular get request $httpBackend.whenGET('/jogasok').respond([ {id: 1, name: 'asdfasdf'}, {id: 2, name: '2wrerwert'} ]); // inject the service to be tested inject(function(_Global_) { Global = _Global_; }); }); it('should exist', function() { expect(!!Global).toBe(true); }); it('getJogasok should return everyone', function() { $httpBackend.flush(); // &lt;------------ need to flush $httpBackend expect(JSON.stringify(Global.getJogasok())).toBe(JSON.stringify([ {id: 1, name: 'asdfasdf'}, {id: 2, name: '2wrerwert'} ])); }); }); </code></pre> <p>In any event, I would rewrite the factory a bit differently, because, as it is currently written, it queries the database only upon instantiation <code>var jogasok = Jogas.query();</code>. Because <a href="http://docs.angularjs.org/guide/dev_guide.services.creating_services" rel="nofollow">services in angularjs are singletons</a>, in your app you will have only the data as they are at the time of instantiation. Therefore, later modifications to the data will not be reflected in your factory. Here is an example of the factory and its unit-test that reflects this idea. </p> <p>the factory:</p> <pre><code>app.factory('GlobalBest', function ($resource) { return $resource('/jogasok/:id', { 'id': '@id' }, { 'ujBerlet': { 'method': 'POST', 'params': { 'berlet': true } } }); }); </code></pre> <p>the test:</p> <pre><code>describe('GlobalBest Service: ', function () { var srv, $httpBackend; beforeEach(module('bkJoga')); beforeEach(function () { // inject the mock for the http backend inject(function (_$httpBackend_) { $httpBackend = _$httpBackend_; }); // inject the service to be tested inject(function (_GlobalBest_) { srv = _GlobalBest_; }); }); it('should exist', function () { expect( !! srv).toBe(true); }); it('query() should return everyone', function () { // mock the response to a particular get request $httpBackend.whenGET('/jogasok').respond([{ id: 1, name: 'asdfasdf' }, { id: 2, name: '2wrerwert' }]); // send request to get everyone var data = srv.query(); // flush the pending request $httpBackend.flush(); expect(JSON.stringify(data)).toBe(JSON.stringify([{ id: 1, name: 'asdfasdf' }, { id: 2, name: '2wrerwert' }])); }); it('get({id: 1}) should return object with id=1', function () { // mock the response to a particular get request $httpBackend.whenGET('/jogasok/1').respond({ id: 1, name: 'asdfasdf' }); var datum = srv.get({ id: 1 }); $httpBackend.flush(); expect(JSON.stringify(datum)).toBe(JSON.stringify({ id: 1, name: 'asdfasdf' })); }); }); </code></pre> <p>I wrote a <kbd><a href="http://jsfiddle.net/klode/zVUL2/" rel="nofollow"><strong>fiddle-demo</strong></a></kbd> with 3 versions of the service: your original service 'Global', a new version 'GlobalNew' that returns the query() method, and finally a version 'GlobalBest' that returns directly the $resource. Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload