English 中文(简体)
如何在安伯底特科林进行单位测试?
原标题:How to run unit tests on Android Firebase Kotlin?

因此,我正在利用防火基地进行聊天。 在执行《观点》和实际碎片之前,我想检查一下我的消防基地储存库是否可行。 我呼吁让用户也这样做。

Fragmet -> ViewModel -> Reito -> FirebaseReito-> 暂停使用 ArrayList

该法典是:

class Repository(val firebaseRepository : FirebaseRepository) {

    suspend fun getAllUsersFromFirebase() : ArrayList<UserInfo> {
        return firebaseRepository.getAllUsers()
    }

}

class FirebaseRepository() {

    private val TAG = "FIREBASE REPOSITORY : "

    private val firebaseAuth:FirebaseAuth = FirebaseAuth.getInstance()
    private val firebaseDatabase :FirebaseDatabase = FirebaseDatabase.getInstance()

    suspend fun getAllUsers() : ArrayList<UserInfo> {
        val ref = firebaseDatabase.getReference("/users")
        val userList = ArrayList<UserInfo>()

        ref.addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                if(snapshot.exists()) {
                    Log.d(TAG,"$snapshot")
                    val tempList = ArrayList<UserInfo>()
                    for(child in snapshot.children){
                        val temp = child.getValue(UserInfo::class.java)
                        if(!temp?.userId!!.equals(firebaseAuth.currentUser!!.uid)) {
                            tempList.add(temp)
                        }
                    }
                }
            }

            override fun onCancelled(error: DatabaseError) {
                Log.d(TAG,"$error ${error.message}")
            }

        })

        if(userList.isEmpty()) {
            userList.add(UserInfo("NO USER FOUND","NO USER FOUND","NO USER FOUND","NO USER FOUND"))
        }

        return userList
    }
}

因此,我如何检验这一点? 我要指出,消防基地可能构成问题。 我认为,我可以处理此事,但我如何检验这一火基呼吁?

最佳回答

我要检查一下我的消防基地存放设施是否可行。

它赢得笔工作,因为由于一种方法,你正在退回<条码>用户List,而这一方法实际上是不可能的,因为消防基地的APIC是同步的。 因此,这里的问题是,你所附的听众在问询结束后被援引一些不为人知的时间,你永远不知道要花多少时间。 根据您的连接速度和国家情况,在获得数据之前,可以从几百毫米秒到几秒。

尽管如此,在援引退约之前,您的<代码>用户List将是空的。 这意味着,你应始终利用反馈中的成果。 我已撰写一篇关于这个专题的文章,呼吁你们理解这一概念:

如果你想进行单位测试,那么我建议你使用mockito 。 并使用以下员额的答复:

P.S. 您的getAllUsers() 方法并不需要暂停使用,因为你没有使用任何等待(......)。 如果你想要使用,我建议你读一下以下条款:

问题回答

你可以尝试:

    // Optional -- Robolectric environment
    const val coreTest = "androidx.test:core:1.5.0"
    // Optional -- Mockito framework
    const val mockito = "org.mockito:mockito-core:5.5.0"
    // Optional -- mockito-kotlin
    const val mockitoKotlin = "org.mockito.kotlin:mockito-kotlin:5.1.0"
    // Optional -- Mockk framework
    const val mockk = "io.mockk:mockk:v1.13.8"

增加上述图书馆。

法典:档案 试验

@Before
    fun setup() {
        MockitoAnnotations.openMocks(HomeViewModelTest::class)
    }

    @Test
    fun getAllUsersFromFirebase() = runBlocking {
        val getRepositoryUseCase = mock(Repository::class.java)

        `when`(getRepositoryUseCase.invoke()).thenReturn(listData())
        val response = getRepositoryUseCase.invoke()

        Assert.assertTrue(response == listUserDummy())
    }




相关问题
run unit tests and coverage in certain python structure

I have some funny noob problem. I try to run unit tests from commandline: H:PROpyEstimator>python src estpython est_power_estimator.py Traceback (most recent call last): File "src est...

How to unit-test an enterprise symfony project?

I´m working on a huge project at my work. We have about 200 database tables, accordingly a huge number of Models, Actions and so on. How should I begin to write tests for this? My biggest problem ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Unit testing. File structure

I have a C++ legacy codebase with 10-15 applications, all sharing several components. While setting up unittests for both shared components and for applications themselves, I was wondering if there ...

Unit Testing .NET 3.5 projects using MStest in VS2010

There s a bug/feature in Visual Studio 2010 where you can t create a unit test project with the 2.0 CLR. https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483891&wa=...

Unit Test for Exceptions Message

Is there a simple (Attribute-driven) way to have the following test fail on the message of the exception. [TestMethod()] [ExpectedException(typeof(ArgumentException))] public void ExceptionTestTest() ...

热门标签