遵循 Detox Mock 实施但不起作用 [英] Followed Detox Mock Implementation but not working

查看:43
本文介绍了遵循 Detox Mock 实施但不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 advance mocking with排毒.我这样做是因为我想模拟我的 api.js 文件,该文件默认从后端服务器获取数据.我命名为 api.e2e.js 的假 api 文件将只包含返回承诺的 json 数据的函数.不幸的是,伪造的 api 没有启动.以下是我使用的库

I am following the Detox documentation on the advance mocking with detox. I am doing so because I would like to mock up my api.js file which by default fetching data from a backend server. My fake api file which I named it api.e2e.js will just contain functions returning promised with json data. Unfortunately, the fake api does not kicks in. Below is my libraries used

  1. 反应原生 0.57.8
  2. 排毒 9.1.2

我也尝试通过 Metro 和 build 命令触发 env 变量,但没有成功.

I have tried to trigger the env variable through the metro and build command as well but with no luck.

action/restaurants.js

action/restaurants.js

import Api from '../api';

export const getRestaurants = () => {
    return (dispatch) => {
        dispatch(gettingRestaurants());
        Api.get.restaurants()
            .then(resJson => {
                console.log('get response', resJson);
                dispatch(getRestrauntsSucces(resJson));
            }).catch(error => {
                console.log('response error', error)
                dispatch(getRestaurantsFailure(error));
            })
    }
}

api.e2e.js

export default {
    get: {
        restaurants: () => {
            console.log('you are in fake api');
            return new Promise((resolve, reject) => {
                resolve( [
                    {
                        id: 1,
                        name: 'Test Shop',
                        location: 'Johore',
                        category: 'Johore',
                        user_id: 1
                    }
                ])
            })
        },


    }

}

rn-cli.config.js

rn-cli.config.js

module.exports = {
    getSourceExts: () => process.env.RN_SRC_EXT ? 
                         process.env.RN_SRC_EXT.split(',') : []
   };

预计会调用伪造的 api 但仍会调用原始 api.

Expected the fake api will be called but still the original api is called.

推荐答案

在运行测试之前,您需要使用 RN_SRC_EXT=e2e.js 启动打包程序.

You need to start your bundler with RN_SRC_EXT=e2e.js before running your tests.

所以流程应该是:运行 RN_SRC_EXT=e2e.js react-native start,然后运行 ​​detox tests.您也可以尝试在 package.json 中使用 RN_SRC_EXT=e2e.js 构建排毒:

So the flow should be: Run RN_SRC_EXT=e2e.js react-native start, then detox tests. You can also try building detox with RN_SRC_EXT=e2e.js in package.json:

    "detox": {
        "configurations": {
            "ios.sim.debug": {
                "binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/Glitz.app",
                "build": " RN_SRC_EXT=e2e.js xcodebuild...YOUR BUILD",
                "type": "ios.simulator",
                "name": "iPhone X"
            }
        },
        "test-runner": "jest"
    }

这篇关于遵循 Detox Mock 实施但不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆