# Building Tests for New discourse\_api Endpoints

**URL:** <https://meta.discourse.org/t/building-tests-for-new-discourse-api-endpoints/118639>\
**Category:** Development\
**Created:** [24.Май.2019 22:21:24 UTC](https://meta.discourse.org/t/building-tests-for-new-discourse-api-endpoints/118639 "2019-05-24T22:21:24Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![kimardenmiller](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kimardenmiller/32/119631_2.png) [@kimardenmiller](https://meta.discourse.org/u/kimardenmiller)\
**Post date:** [24.Май.2019 22:21:24 UTC](https://meta.discourse.org/t/building-tests-for-new-discourse-api-endpoints/118639/1 "2019-05-24T22:21:24Z")

</div>

Adding some `polls` API endpoints for PR to discourse\_api, which work fine. Now I’m trying to understand how to create tests before submitting the PR, e.g.:

```
require 'spec_helper'

describe DiscourseApi::API::Polls do
  subject { DiscourseApi::Client.new("http://localhost:3000", "test_d7fd0429940", "test_user" )}

  describe "#polls" do
    before do
      stub_get("http://localhost:3000/polls/voters.json").to_return(body: fixture("voters.json"), headers: { content_type: "application/json" })
    end

    it "requests the correct resource" do
      subject.voters :post_id => 27885, :poll_name => 'poll' 
      expect(a_get("http://localhost:3000/polls/voters.json")).to have_been_made
    end
  end
end

```

But am getting the error:

```
   Failed to open TCP connection to localhost:3000 (Connection refused - connect(2) for "localhost" port 3000)
 # ./lib/discourse_api/client.rb:141:in `rescue in request'
 # ./lib/discourse_api/client.rb:132:in `request'
 # ./lib/discourse_api/client.rb:85:in `get'
 # ./lib/discourse_api/api/polls.rb:22:in `voters'
 # ./spec/discourse_api/api/polls_spec.rb:12:in `block (3 levels) in <top (required)>'
 # ------------------
 # --- Caused by: ---
 # Errno::ECONNREFUSED:
 # Connection refused - connect(2) for "localhost" port 3000
 # /Users/kimardenmiller/.rbenv/versions/2.5.3/gemsets/d_api/gems/webmock-2.3.2/lib/webmock/http_lib_adapters/net_http.rb:109:in `request'

```

All the existing tests pass fine for me, and I’m trying to copy the format of those existing tests, but cannot for the life of me figure out what I’m doing wrong.

Here’s the (new) endpoint being tested:

```
module DiscourseApi
  module API
    module Polls
     def poll_voters(args)
        args = API.params(args) # post_id, poll_name, user, opts = {}
                  .required(:post_id, :poll_name)
                  .optional(:opts, :api_username)
        response = get("/polls/voters.json", args)
        response[:body]
      end
    end
  end
end

```

---

_[View the full topic](https://meta.discourse.org/t/building-tests-for-new-discourse-api-endpoints/118639)._
