How to mock Firebase in Karma tests for Angular app

Following the AngularFire tutorial, I synchronized the scope variable with the Firebase array. My code is basically the same as in the tutorial (step 5):

https://www.firebase.com/docs/web/libraries/angular/quickstart.html

Everything in my application works, but I really got confused about how to properly mock the Firebase call in my Karma unit tests. I assume something like using $ provides a mockery of the data? But then $ add will not work in my controller methods. Help?

+3
angularjs karma-runner firebase angularfire
source share
1 answer

Copied from this meaning , which discusses this issue in detail.

The comments are all inclusive. There is much to consider.

What is wrong with mocks

Consider this data structure used to get a membership-based list of names.

/users/<user id>/name /rooms/members/<user id>/true 

Now, let's create a couple of simple classes without any real consideration for testing structures, assuming we will use the Firebase layout to test them. Notice that I see such errors constantly in the wild; this example is not too exaggerated or exaggerated (it is pretty tame in comparison)

 class User { // Accepts a Firebase snapshot to create the user instance constructor(snapshot) { this.id = snapshot.key; this.name = snapshot.val().name; } getName() { return this.name; } // Load a user based on their id static load(id) { return firebase.database().ref('users').child(uid) .once('value').then(snap => new User(snap)); } } class MembersList { // construct a list of members from a Firebase ref constructor(memberListRef) { this.users = []; // This coupling to the Firebase SDK and the nuances of how realtime is handled will // make our tests pretty difficult later. this.ref = memberListRef; this.ref.on('child_added', this._addUser, this); } // Get a list of the member names // Assume we need this for UI methods that accept an array, so it can't be async // // It may not be obvious that we've introduced an odd coupling here, since we // need to know that this is loaded asynchronously before we can use it. getNames() { return this.users.map(user => user.getName()); } // So this kind of stuff shows up incessantly when we couple Firebase into classes and // it has a big impact on unit testing (shown below) ready() { // note that we can't just use this.ref.once() here, because we have to wait for all the // individual user objects to be loaded, adding another coupling on the User class internal design :( return Promise.all(this.promises); } // Asynchronously find the user based on the uid _addUser(memberSnap) { let promise = User.load(memberSnap.key).then(user => this.users.push(user)); // note that this weird coupling is needed so that we can know when the list of users is available this.promises.push(promise); } destroy() { this.ref.off('child_added', this._addUser, this); } } /***** Okay, now on to the actual unit test for list names. ****/ const mockRef = mockFirebase.database(/** some sort of mock data */).ref(); // Note how much coupling and code (ie bugs and timing issues we might introduce) is needed // to make this work, even with a mock function testGetNames() { const memberList = new MemberList(mockRef); // We need to abstract the correct list of names from the DB, so we need to reconstruct // the internal queries used by the MemberList and User classes (more opportunities for bugs // and def. not keeping our unit tests simple) // // One important note here is that our test unit has introduced a side effect. It has actually cached the data // locally from Firebase (assuming our mock works like the real thing; if it doesn't we have other problems) // and may inadvertently change the timing of async/sync events and therefore the results of the test! mockRef.child('users').once('value').then(userListSnap => { const userNamesExpected = []; userListSnap.forEach(userSnap => userNamesExpected.push(userSnap.val().name)); // Okay, so now we're ready to test our method for getting a list of names // Note how we can't just test the getNames() method, we also have to rely on .ready() // here, breaking our effective isolation of a single point of logic. memberList.ready().then(() => assertEqual(memberList.getNames(), userNamesExpected)); // Another really important note here: We need to call .off() on the Firebase // listeners, or other test units will fail in weird ways, since callbacks here will continue // to get invoked when the underlying data changes. // // But we'll likely introduce an unexpected bug here. If assertEqual() throws, which many testing // libs do, then we won't reach this code! Yet another strange, intermittent failure point in our tests // that will take forever to isolate and fix. This happens frequently; I've been a victim of this bug. :( memberList.destroy(); // (or maybe something like mockFirebase.cleanUpConnections() }); } 

Better Approach Using Proper Encapsulation and TDD

Ok, now let me reverse this by starting with developing an effective test unit and see if we can design our classes with lesser grip to accommodate.

 function testGetNames() { const userList = new UserList(); userList.add( new User('kato', 'Kato Richardson') ); userList.add( new User('chuck', 'Chuck Norris') ); assertEqual( userList.getNames(), ['Kato Richardson', 'Chuck Norris']); // Ah, this is looking good! No complexities, no async madness. No chance of bugs in my unit test! } /** Note how our classes will be simpler and better designed just by using a good TDD */ class User { constructor(userId, name) { this.id = userId; this.name = name; } getName() { return this.name; } } class UserList { constructor() { this.users = []; } getNames() { return this.users.map(user => user.getName()); } addUser(user) { this.users.push(user); } // note how we don't need .destroy() and .ready() methods here just to know // when the user list is resolved, yay! } // This all looks great and wonderful, and the tests are going to run wonderfully. // But how do we populate the list from Firebase now?? The answer is an isolated // service that handles this. class MemberListManager { constructor( memberListRef ) { this.ref = memberListRef; this.ref.on('child_added', this._addUser, this); this.userList = new UserList(); } getUserList() { return this.userList; } _addUser(snap) { const user = new User(snap.key, snap.val().name); this.userList.push(user); } destroy() { this.ref.off('child_added', this._addUser, this); } } // But now we need to test MemberListManager, too, right? And wouldn't a mock help here? Possibly. Yes and no. // // More importantly, it just one small service that deals with the async and external libs. // We don't have to depend on mocking Firebase to do this either. Mocking the parts used in isolation // is much, much simpler than trying to deal with coupled third party dependencies across classes. // // Additionally, it often better to move third party calls like these // into end-to-end tests instead of unit tests (since we are actually testing // across third party libs and not just isolated logic) // // For more alternatives to a mock sdk, check out AngularFire. // We often just used the real Firebase Database with set() or push(): // https://github.com/firebase/angularfire/blob/master/tests/unit/FirebaseObject.spec.js#L804 // // An rely on spies to stub some fake snapshots or refs with results we want, which is much simpler than // trying to coax a mock or SDK to create error conditions or specific outputs: // https://github.com/firebase/angularfire/blob/master/tests/unit/FirebaseObject.spec.js#L344 
0
source share

All Articles