首页 > 解决方案 > 对临时站点的 POST 请求如何工作?

问题描述

我正在开发一个使用 Ruby on Rails 和 GraphQL API 编写的应用程序。在我的测试文件中,我向我们的暂存站点发送了一个 POST 请求,以执行登录突变。这是测试文件:

require 'test_helper'

class Mutations::SignInUserTest < ActionDispatch::IntegrationTest
    def perform(args = {})
        Mutations::SignInUser.new(object: nil, field: nil, context: {}).resolve(args)
    end

    test 'sign in user' do
        post "apistaging.app.com/graphql"

        user = perform(
            auth: {
                user_id: "example",
                password: "example"
            }
        )

        assert user.persisted?
    end
end

但是,在 中test.log,这是我跑步时看到的rails test

Started POST "/graphql" for 127.0.0.1 at 2021-06-16 00:06:27 +0700
Processing by GraphqlController#execute as HTML
Completed 200 OK in 32ms (Views: 0.3ms | ActiveRecord: 0.0ms)

请注意,我的根目录中有一个 Dockerfile 和一个 docker-compose.yml,docker-compose up然后运行docker exec到我们的应用程序的容器中运行rails test。日志是否意味着 POST 请求正在发送到我的 localhost:3000/graphql?我问是因为登台数据库应该有用于登录的用户数据,但由于某种原因,当我运行测试时数据库是空的,所以所有查询和突变都返回 null。我的根目录中还有一个 .sql 文件,有没有办法将文件中的数据导入到我的数据库中以便进行测试?

标签: ruby-on-railsdockerunit-testinggraphql

解决方案


我认为你错过的第一件事是docker-compose build。之后你应该docker-compose up。此外,您还必须知道您的 docker 配置到哪个端口。默认值为 80,但是您的端口可能已经在使用中,将 docker 端口保留为 80 并不是一个好习惯。您应该在您的端口中执行以下操作Dockerfile

EXPOSE 5050 #or any other port you want
CMD ["rails", "server", "-b", "0.0.0.0"]

EXPOSE 5050公开本地机器上的端口 5050 供 docker 使用 CMD ["rails", "server", "-b", "0.0.0.0"],让您可以使用其网络中的任何 IP 地址,您不必担心管理 IP 地址

在你的docker-compose.yml

your-api:
    ports:
      - 5050:5050
   

因此,当docker-compose build运行时,它将组成您的 docker 并打开端口 5050 供其使用,这样您就可以访问localhost:5050并访问您的 dockerised api 和端点。

这样你就可以确定你的 POST 请求被发送到 docker api 当你POST localhost:5050/graphql

请查看 docker 以获取 rails 文档,这会有所帮助


推荐阅读