How to mock AngularFire 2 service in unit test?

前端 未结 3 1825
情话喂你
情话喂你 2021-02-05 12:14

I\'m trying to set up unit tests for a sample Angular 2 app using AngularFire 2 auth, the component is fairly simple:

import { Component } from \'@angular/core\'         


        
3条回答
  •  悲&欢浪女
    2021-02-05 12:48

    hope it is not off the topic, but the easiest solution I have found how to mock the FirebaseDatabase.

    var object = function() {
          var obj = { valueChanges() {
                return of({data:'data'});     
            }
          }
          return obj;
        }
    
    providers: [..., { provide : AngularFireDatabase,
            useValue: {object : object }} ]
    

    instead of data:'data' you can mock whatever data you need. The functions can be modified as you wish.

提交回复
热议问题