Friedrich Ewald My Personal Website

Ruby Testing Server Part Two

Following from my post from 2022 about testing Rails application with a static server, I wanted to mock an API during a test. The reason for this is that I like to keep the external dependencies to a minimum. Webrick supports mount_proc which lets me create endpoints on the fly. Since my fixture_server provides access to the server object, it’s as simple as this:

fixture_server do |server, port|
  server.mount_proc '/extract_author' do |req, res|
    res.body = { authors: %w[Alice Bob] }.to_json
  end
  nlp = Nlp.new("http://localhost:#{port}")
  extracted_authors = nlp.extract_author("Alice, Bob")
  assert_not_nil extracted_authors
  assert_equal %w[Alice Bob], extracted_authors
end


About the author

is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.