小编air*_*ine的帖子

如何测试 google.maps.Geocoder?

你好!我正在尝试编写一个“简单”的测试来检查反应类组件状态的变化。具体来说,我正在测试 lat(latitude) 和 lng(longitude) 状态是否会在 Google 成功对我发送的某些字符串(地址)进行地理编码后发生变化。这是我要测试的示例(i.e. the lat and lng states being set to results[0].geometry.location.lat())

getLatLong = (address) => {
    const that = this;
    var geo = new google.maps.Geocoder;

    geo.geocode({'address':address},(results, status) => {
        if (status == google.maps.GeocoderStatus.OK) {
            that.setState(
            {
                center: {
                    lat: results[0].geometry.location.lat(),
                    lng: results[0].geometry.location.lng(),
                },
            });
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在我的笑话套件中,我在编写测试和监视/模拟 google.maps.Geocoder 时遇到问题,因为从未导入 google 库。它通过脚本附加,如下所示:

<script async defer
    src=<%="https://maps.googleapis.com/maps/api/js?key=#{Rails.application.config.google_api_key}&callback=initMap"%>>
</script>
Run Code Online (Sandbox Code Playgroud)

就这样确认了,代码在手动测试后按预期工作,但是当我尝试像这样进行间谍时,我的测试套件中收到错误:

let geocoderSpy = jest.spyOn(google.maps, 'Geocoder');
Run Code Online (Sandbox Code Playgroud)

我收到这样的错误:

  ? EventMap › getLatLong works as intended by getting …
Run Code Online (Sandbox Code Playgroud)

javascript testing google-maps reactjs jestjs

4
推荐指数
1
解决办法
3490
查看次数

收到 TypeError: n.getChildContext 不是带有shallowWithIntl的函数

几周前,我和同事开始了一个使用 React 前端和 ruby​​ 后端的项目。经过几次代码审查后,有关应用程序未国际化的评论引起了我们的注意。我们有一个使用 i18n gem 的国际化后端,但被告知 React 的标准是使用 React-intl 作为前端国际化包。我刚刚完成了国际化的编码,并使用几种语言对其进行了测试以确保其正常工作。在测试这个话题上,我开始遇到一个我一直在用头撞墙的问题。 我不断收到此错误:n.getChildContext 不是函数。我正在使用enzyme-react-intl包。为了测试这是否有效,我决定只从拍摄我的组件(功能组件和基于类的组件)的快照开始。下面是我的一项测试的示例以及我收到的测试套件失败情况。我所有的测试套件都带有shallowWithIntlmountWithIntl的测试套件都失败了。我应该注意,我正在使用命令运行测试:\'yarn jest -u\'。从我读过的所有搜索和 api 文档中,我没有做出任何明显的错误,但如果对答案有任何帮助,我将不胜感激。

\n\n

这是一个测试示例:

\n\n
import React from \'react\';\nimport { loadTranslation, shallowWithIntl } from \'enzyme-react-intl\';\nimport Header from \'../components/Header/Header\';\nloadTranslation("./app/javascript/translations/en-US.json");\n\ndescribe(\'Parent header rendering\', () => {\n     const shallowHeader = shallowWithIntl(<Header />);\n     it(\'matches the snapshot\', () => {\n         expect(shallowHeader).toMatchSnapshot();\n     });\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

我收到的测试套件错误。

\n\n

应用程序/javascript/测试失败 /Header.test.jsx\n \xe2\x97\x8f 父标头渲染 \xe2\x80\xba 遇到声明异常

\n\n
TypeError: n.getChildContext is not a function\n\n   5 …
Run Code Online (Sandbox Code Playgroud)

internationalization reactjs jestjs enzyme react-intl

1
推荐指数
1
解决办法
1610
查看次数

如何测试控制器中的实例变量?

我的几个导师告诉我用控制器规范测试一段代码,即使内容正在通过一些请求规范进行测试。我想知道是否可以测试是否@user可以在 rspec 控制器测试中设置someObject或设置实例变量nil

到目前为止,我已经尝试了几种不同的方法来测试这个,但我在 rspec 测试方面的技能至少可以说是初级的。我尝试的第一件事是模拟 current_user 和 'User.get_from_auth_user(current_user)。模拟对象打印良好并输出。问题是......我希望能够说出类似的话,expect(@user).to equal(mockedUser)但这似乎永远不会奏效。我总是得到类似的东西:

Expect does not equal received
expected is nil
received is { someMockedObject }
Run Code Online (Sandbox Code Playgroud)

这是我要测试的代码。

class ApplicationController < ActionController::Base
  before_action :set_user

  def set_user
    return if current_user.nil?

    @user = User.get_from_auth_user(current_user)
  end
end
Run Code Online (Sandbox Code Playgroud)

我想知道我是否走在正确的道路上,或者什么是完全测试这块代码的更好选择。我觉得答案应该很简单。

ruby testing unit-testing rspec ruby-on-rails

1
推荐指数
1
解决办法
1372
查看次数