fingular
Unit test your Angular/Firebase applications using a mocked-out Firebase provider.
What's this for?
So you're writing your amazing new web application using Angular and Firebase. Because you're a good developer, you want to write unit tests that don't need to connect to a real Firebase instance to run correctly. So you need a mock for Firebase. Luckily, one already exists: MockFirebase.
But how do you seamlessly switch between using MockFirebase in your tests, and real Firebase in production? Simple. Use Angular's own dependency injection (DI) framework.
How's it work?
- Include Fingular in your Javascript build.
- Add the Fingular module to your own module's dependencies.
- Configure Fingular either by using the provider object or by setting module values.
Now you can inject the $firebaseRef and $firebaseUser objects anywhere in Angular. $firebaseRef is a proxy for new Firebase(url);
, and $firebaseUser is a proxy for new FirebaseSimpleLogin(method, options);
.
$firebaseRef
Usage
- $firebaseRef takes care of the domain name part of the Firebase connection. All you have to supply is the key path, like so:
$firebaseRefProvider;...;
This way you can easily change which Firebase instance your app points to for purposes of integration testing or migration.
-
Depending on configuration, $firebaseRef is either a proxy to Firebase or a mock object like MockFirebase. An example of how to do this is provided in examples/myApp.js and examples/myApp_test.js.
-
Although a function, $firebaseRef has three read-only properties for introspection:
protocol
: eitherhttp
orhttps
.domain
: the domain the firebaseRef points to.mocked
: boolean. Tells whether the $firebaseRef is mocked out.
Configuration
You can configure $firebaseRef either by setting values in your Angular module or by configuring $firebaseRefProvider. Settings on the provider override the values.
Values
firebaseDomain
: the domain name of the Firebase instance to point to.firebaseProtocol
: Eitherhttp
orhttps
. Defaults to https.firebaseMock
: a mock constructor to substitute for Firebase.firebaseMockData
: A plain old Javascript object that Fingular will interrogate and use to provision the Firebase mock object with data.
Example use:
angularvalue'firebaseDomain' 'metropolis.firebaseio.com'value'firebaseProtocol' 'http' // Don't ever do this. Have you learned nothing, Fritz?value'firebaseMock' MockFirebasevalue'firebaseMockData' 'users': 'fred': 'name': 'Freder Frederson' 'hometown': 'Metropolis' ;
$firebaseRefProvider
domain(domainName)
: the domain name of the Firebase instance to point to.protocol(protocolName)
: Eitherhttp
orhttps
. Defaults to https.mockWith(constructorFunction)
: a mock constructor to substitute for Firebase.mockOut(keyPath, value)
: Have the mock Firebase object supply the given data for the given path. Currently only works with MockFirebase. (If you wish to use your own mock,value
will be supplied as a second argument to the mock constructor function.
Example use:
angular;
$firebaseUser
You can use $firebaseUser in place of FirebaseSimpleLogin to obtain some advantages vis-a-vis testing and some bonus Angular integration to boot.
Usage
angular;
Configuration
You can configure $firebaseUser either by setting values in your Angular module or by configuring $firebaseUserProvider. Settings on the provider override the values.
Values
firebaseUserMock
: The mock user constructor to substitute for FirebaseSingleLogin. Ordinarily you'll want to use MockFirebaseSimpleLogin.firebaseUserMockData
: user data to be handed to the mocking framework.
Notes
Thanks to firebase-debug, on npm test
you'll see this error:
Error loading resource fingular/test/deps.js (203). Details: Error opening fingular/test/deps.js: No such file or directory
You can just ignore that.
License
MIT