Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
632 views
in Technique[技术] by (71.8m points)

elixir - How to spawn a cowboy server in a test?

Background

I have a specific test where I need to spawn a cowboy server listening on port 8082 that returns some static responses.

Objective

Normally I would spin up a cowboy process in my application.ex file like this:

   def start(_type, args) do
     children = children([{Cowboy, scheme: :http, plug: MyServer, options: [port: 8082]}])

     opts = [strategy: :one_for_one, name: MyApp.Supervisor]
     Supervisor.start_link(children, opts)
   end

But the code I am working on is not an application, so I can't do that.

My objective is to use the setup_all callback of ExUnit to do this, however I don't know how to do it or if it is possible.

Questions

  1. How can I start a cowboy server in ExUnit?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After some digging around, I found that there is a library called bypass that already covers this use case.

This solution was also confirmed by Elixir's community, which seems to suggest it as well: https://elixirforum.com/t/how-to-spawn-a-cowboy-server-in-a-test/36686


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...