Created
February 25, 2014 09:39
-
-
Save achambers/9205855 to your computer and use it in GitHub Desktop.
Unit testing an arbitrary Ember.Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default Ember.Object.extend({ | |
location: function() { | |
return new Ember.RSVP.Promise(function(resolve, reject) { | |
//Simulate some async operation | |
Ember.run.later(function() { | |
resolve({lat: "123", lon: "234", city: "London"}); | |
}, 2000); | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { test } from 'appkit/tests/helpers/module-for'; | |
import Client from 'appkit/clients/location'; | |
module('Unit - LocationClient', { }); | |
test("it returns the lat, long and city", function() { | |
//expect(3); | |
var subject = Client.create(), | |
result; | |
Ember.run(function() { | |
result = subject.location(); | |
}); | |
//If I do it this way, the assertions don't wait for the location() Promise to resolve, obviously | |
equal(result.lat, "123"); | |
equal(result.lon, "234"); | |
equal(result.city, "London"); | |
//So I tried this...Now the test waits for the location() Promise to resolve, but I get errors | |
//saying that lat, long, city were undefined. Makes sense because result is a Promise | |
andThen(function() { | |
equal(result.lat, "123"); | |
equal(result.lon, "234"); | |
equal(result.city, "London"); | |
}); | |
//So then I tried this. But again, the test doesn't wait for the Promise to resolve and therefore, | |
//no assertions are made. | |
result.then(function(payload) { | |
equal(payload.lat, "123"); | |
equal(payload.lon, "234"); | |
equal(payload.city, "London"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment