在JEST中抓块

问题描述

我了解如何做一条快乐的路,并且我所有的快乐路都有代码覆盖,但是我在弄清楚如何处理catch时遇到了麻烦。我目前正在使用supertest

例如,我有这个API

router.get("/all",checkAuth,(req,res) => {

    Post.find({},{ _id: 0 }).select('id title body _id userId').then((data) => {
        status200("posts retrieved successfully",data,res)

    }).catch((err) => {
        status500(err,"Failed to retrieve posts",res);
    })

})

快乐路径示例


    describe('GET - /posts',() => {

        const exec = async () => {
            return await request(server)
                .get('/api/posts/all')
                .set('x-auth-token',token);
        }


        it('GETs posts and returns status 200',async () => {
            const user = new User({
                name: "John Smith",email: "johnsmith@gmail.com"
            });

            await user.save();


            const post1 = new Post({
                title: "Title 1",body: "body 1",userId: user._id
            });


            const post2 = new Post({
                title: "Title 2",body: "body 2",userId: user._id
            });


            await post1.save();
            await post2.save();


            const res = await exec();
            expect(res.status).toBe(200);
            expect(res.body.data.length).toBe(2);
            expect(res.body.data.some(p => p.title === 'Title 2')).toBeTruthy();
            expect(res.body).toHaveProperty('email',post2.email);
        });

    });

如何为catch块编写JEST测试?

解决方法

您可以使用jest.spyOn方法来模拟package androidx.appcompat.app import android.content.Context import android.content.res.Configuration import android.os.Bundle import android.util.AttributeSet import android.view.MenuInflater import android.view.View import android.view.ViewGroup import androidx.appcompat.view.ActionMode import androidx.appcompat.widget.Toolbar class BaseContextWrappingDelegate(private val superDelegate: AppCompatDelegate) : AppCompatDelegate() { override fun getSupportActionBar() = superDelegate.supportActionBar override fun setSupportActionBar(toolbar: Toolbar?) = superDelegate.setSupportActionBar(toolbar) override fun getMenuInflater(): MenuInflater? = superDelegate.menuInflater override fun onCreate(savedInstanceState: Bundle?) { superDelegate.onCreate(savedInstanceState) removeActivityDelegate(superDelegate) addActiveDelegate(this) } override fun onPostCreate(savedInstanceState: Bundle?) = superDelegate.onPostCreate(savedInstanceState) override fun onConfigurationChanged(newConfig: Configuration?) = superDelegate.onConfigurationChanged(newConfig) override fun onStart() = superDelegate.onStart() override fun onStop() = superDelegate.onStop() override fun onPostResume() = superDelegate.onPostResume() override fun setTheme(themeResId: Int) = superDelegate.setTheme(themeResId) override fun <T : View?> findViewById(id: Int) = superDelegate.findViewById<T>(id) override fun setContentView(v: View?) = superDelegate.setContentView(v) override fun setContentView(resId: Int) = superDelegate.setContentView(resId) override fun setContentView(v: View?,lp: ViewGroup.LayoutParams?) = superDelegate.setContentView(v,lp) override fun addContentView(v: View?,lp: ViewGroup.LayoutParams?) = superDelegate.addContentView(v,lp) override fun attachBaseContext2(context: Context) = wrap(superDelegate.attachBaseContext2(super.attachBaseContext2(context))) override fun setTitle(title: CharSequence?) = superDelegate.setTitle(title) override fun invalidateOptionsMenu() = superDelegate.invalidateOptionsMenu() override fun onDestroy() { superDelegate.onDestroy() removeActivityDelegate(this) } override fun getDrawerToggleDelegate() = superDelegate.drawerToggleDelegate override fun requestWindowFeature(featureId: Int) = superDelegate.requestWindowFeature(featureId) override fun hasWindowFeature(featureId: Int) = superDelegate.hasWindowFeature(featureId) override fun startSupportActionMode(callback: ActionMode.Callback) = superDelegate.startSupportActionMode(callback) override fun installViewFactory() = superDelegate.installViewFactory() override fun createView(parent: View?,name: String?,context: Context,attrs: AttributeSet): View? = superDelegate.createView(parent,name,context,attrs) override fun setHandleNativeActionModesEnabled(enabled: Boolean) { superDelegate.isHandleNativeActionModesEnabled = enabled } override fun isHandleNativeActionModesEnabled() = superDelegate.isHandleNativeActionModesEnabled override fun onSaveInstanceState(outState: Bundle?) = superDelegate.onSaveInstanceState(outState) override fun applyDayNight() = superDelegate.applyDayNight() override fun setLocalNightMode(mode: Int) { superDelegate.localNightMode = mode } override fun getLocalNightMode() = superDelegate.localNightMode private fun wrap(context: Context): Context { //Put wrapping implementation here } } 函数,模拟会引发错误

find

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...