- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Enzyme 和 Jest 测试 React Native 组件。我已经能够测试是否调用了模拟函数(在本例中为 Alert.alert),如下所示:
Alert.alert = jest.fn();
someButton.simulate('Press');
expect(Alert.alert.mock.calls.length).toBe(1);
这种方法效果很好。
无论如何,我有一个登录按钮,它启动一个获取。我的 fetch 函数是这样的:
fetch(ipAddress, {
...
})
.then(response => response.json())
.then((responseJson) => {
if (responseJson.login === 'success') {
Alert.alert('Login', 'Logged in succesfully!');
console.log('i'm here');
我用 promises mock 了一个 fetch。我将控制台打印添加到我的 fetch 函数中,并注意到它们打印在测试用例中,正如我预期的那样。这意味着在测试运行时打印“我在这里”。
无论如何,当我在测试用例中模拟登录按钮按下时,Alert.alert.mock.calls.length 为零。我在这里做错了什么?
最佳答案
我没有用 React Native 检查这个,但我确实在 React 中为服务调用编写了一些测试(我确实使用了你没有使用的 Flux - 但没关系,在不同的地方它是相同的原理)。本质上,当您执行 expect
时,promise 链尚未完成。这意味着,Alert
和 console.log
都在 expect
之后执行,因为默认的 promise 实现将所有后续步骤放在事件队列的末尾。
克服这个问题的一种方法是使用 https://www.npmjs.com/package/mock-promises - 您规范中的 beforeEach
方法需要调用 install
,如下所示:
beforeEach(() => {
Q=require('q');
mp=require('mock-promises');
mp.install(Q.makePromise);
mp.reset();
// more init code
});
别忘了
afterEach(() => {
mp.uninstall();
});
如果您不使用 Q(我当时使用),上面的链接为您提供了如何安装其他 promise 的说明。
现在您有 promise 不会将事情放到事件队列的末尾,您可以通过调用 mp.tick()
来调用下一个 then
。在你的情况下,这将是
it("...", () => {
Alert.alert = jest.fn();
someButton.simulate('Press');
mp.tick();
mp.tick(); // then and then
expect(Alert.alert.mock.calls.length).toBe(1);
});
另一种方式,开箱即用是用期望
和返回附加另一个then
整个 promise 。您可以在此处找到详细信息:https://facebook.github.io/jest/docs/en/tutorial-async.html
基本上,这就是它的样子:
functionReturningPromise = () => {
// do something
return thePromise;
}
// now testing it
it("...", () => {
return /* !!! */ functionReturningPromise().then(() => {
expect(/*something*/).toBeSth();
});
});
但是,在您的情况下,这会很困难,因为您无法在测试代码中处理 promise 。但是,您可以将所有获取逻辑拆分成一个专用方法(至少返回用于测试的 promise )并为此编写测试。
关于javascript - 如何在异步函数中测试 jest.fn().mock.calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48975425/
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
我有一段代码是这样的: while(count = inputStream.readLine()) != null) { //do something } 在单元测试用例中,我正在模拟 inp
假设我在文件中具有以下命名导出 customer.ts export const saveDetails = ()=>{} export const loadDetails = ()=>{} 假设我在
我一直试图绕过dart的模拟库,但似乎我仍然不明白。 在我的库中,我有一个对外部资源的HTTP请求,我想以此模拟它不要一直依赖外部资源。 我的库中的主类如下所示: SampleClass(String
使用 postman 模拟服务器存在问题。它不响应具有路由参数的请求。例如,我的 uri 如下所示: PUT : {{server_url}}/order/{id} 但是当我以这种方式调用模拟服务器时
将任何转换器或空属性转换添加到 Jest 配置时,模拟无法正常工作。下面是简单的代码。 Jest 配置: "transform": { any regex: any transformer } 模块说
我最近一直在做一个项目,该项目已经开始变得相当依赖,并且一直在探索使用 AutoMocking 容器来清理我的测试并使其不那么脆弱的想法。 我听说过 TDD/BDD 纯粹主义者反对使用它们的论点,例如
有谁知道为什么 UsernameExists 不会返回 True。我的语法一定在某个地方搞砸了。 [TestMethod()] public void GenerateUsername
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我得到一个 Moq 对象以在连续调用方法时返回不同的值。这是通过此扩展方法完成的: public static void ReturnsInOrder(this ISetup setup, param
rhino-mocks stub 和这里的期望有什么区别:在我看来它们的行为完全相同? mockContext.Stub(x => x.Find()) .Return(new List()
我正在尝试模拟方法 extra_get() 的调用,该方法通常返回一个字典列表。据我从模拟docs了解,如果我想返回iterable,我应该设置side_effect参数。 client.extra_
在我的 CentOS 6.2 机器的/var/lib/mock 文件夹下,我可以看到目标构建操作系统中的所有初始应用程序。如果我想添加 JDK 作为额外的应用程序,我该怎么做?谢谢! 最佳答案 只需将
我正在寻找一种让 stub 的返回值取决于其输入的干净方法。 目前我正在使用以下方法,但效果不佳。 metadataLogic.Expect(x => x.GetMake(args.Vehicle1.
我正在设置调用构建和执行查询的方法的期望。我想询问所用参数的属性。这可能吗 using (mocks.Record()) { Expect.Call(connection.Retrieve(S
有没有人有一个使用 Rhino Mocks 和 NInject 的自动模拟容器的实现? 最佳答案 好的,我使用 Moq 集成作为起点自己构建了一个。这很简单。你需要这 3 个类: public cla
使用后@patch在我的装饰器上它不再起作用了。我想进行一个失败并引发异常的调用,以便我可以检查我的装饰器是否正在捕获此异常,并正在调用某个函数。 mock do_sth_in_db让它引发异常是很容
谁能告诉我为什么下面的测试没有失败? [Test] public void uhh_what() { var a = MockRepository.GenerateMock(); a.
我找不到 Moq 和 Rhino 的具体功能比较。所有的问题都是“你更喜欢哪个以及为什么”,或者“这是你如何在 rhino 中进行简单的模拟以及如何在最小起订量中完成的”。 我在任何地方都找不到深入的
我正在尝试模拟数据存储库对象,但在 MockRepository 上设置期望后,它每次都返回 null。我的代码如下: [测试] 公共(public)无效 GetById_NotNull() { 预
我是一名优秀的程序员,十分优秀!