I can't seem to return the expected results after stubbing $httpBackend, and I don't know what is wrong with the test. Can you please take a look at this failing minimal example?
http://jsfiddle.net/paos/kTNF5/
query() Expected [ ] to equal [ { hello : 'world' } ].
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine-html.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="http://code.angularjs.org/1.0.1/angular-mocks-1.0.1.js"></script>
Test javascript
var mod = angular.module('mod', ['ngResource']);
mod.factory('Brief', function($resource) {
var Brief = $resource('http://some_test/:id');
return Brief;
});
beforeEach(function() {
module('mod');
inject(function($injector) {
resource = $injector.get('$resource');
$httpBackend = $injector.get('$httpBackend');
Brief = $injector.get('Brief');
});
});
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe("Brief", function() {
it("query()", function() {
var getRequest = 'http://some_test';
var fakeGetResponse = [{
"hello": "world"}];
$httpBackend.when('GET', getRequest).respond(fakeGetResponse);
var briefs = Brief.query();
expect(briefs).toEqual(fakeGetResponse);
$httpBackend.flush();
});
});
// KICK OFF JASMINE
var jasmineEnv = jasmine.getEnv();
var trivialReporter = new jasmine.TrivialReporter();
jasmineEnv.addReporter(trivialReporter);
jasmineEnv.specFilter = function(spec) {
return trivialReporter.specFilter(spec);
};
jasmineEnv.execute();
Thanks!
You've been almost there, but still there were 2 issues in your test:
$httpBackend
was flushed. It is the $httpBackend.flush();
that simulates an incoming response$resource
will create a new array as described in https://stackoverflow.com/a/11966512/1418796. You need to compare objects for their values not references. One way of doing so is: expect(angular.equals(briefs, fakeGetResponse)).toBeTruthy();
Finally here is a jsFiddle with a test passing: http://jsfiddle.net/VFqFU/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With