I would like to test a factory method that runs $resource. My test would mock the real backend that does not exist yet.
Here is a sample factory code:
app.factory( 'Global', function( $resource ){
var Jogas = $resource('/jogasok/:id', {'id': '@id'}, {
'ujBerlet': {'method': 'POST', 'params': {'berlet': true}}
});
var jogasok = Jogas.query();
return {
getJogasok: function() {
return jogasok;
}
};
})
My test would be to check that query was made.
If I initialize my app with:
app.run( function run ($httpBackend) {
$httpBackend.whenGET('/jogasok').respond([
{id: 1, name: 'asdfasdf'},
{id: 2, name: '2wrerwert'}
]);
})
and open the app in my browser, then everything seems to be fine, I have the dummy data in the browser.
But, when I take out the run code above, and write a test, things just don't work.
describe( 'Global Service: ', function() {
beforeEach( module('bkJoga') );
beforeEach( inject(function($httpBackend) {
$httpBackend.whenGET('/jogasok').respond([
{id: 1, name: 'asdfasdf'},
{id: 2, name: '2wrerwert'}
]);
}));
it('getJogasok should return everyone', inject(function(Global) {
expect(JSON.stringify(Global.getJogasok())).toBe(JSON.stringify([
{id: 1, name: 'asdfasdf'},
{id: 2, name: '2wrerwert'}
]));
}));
});
fails.