emacs-gitlab icon indicating copy to clipboard operation
emacs-gitlab copied to clipboard

Mocking `request` in the tests

Open chrycheng opened this issue 7 years ago • 0 comments

There is no mocking in the tests currently. One would need to set up a GitLab account and set up the expected data order to be able to run all of the tests successfully. Mocking request would allow us to uncouple the tests from an actual GitLab instance. We will only need to verify that the correct arguments are passed to request and to ensure that the expected values are returned. We would need to migrate the tests to emacs-buttercup. It's the only Emacs test framework I'm aware of that has mocking capabilities. Here is how test-list-issues would look like, for example:

(describe "Issues API"

  :var (expected-response)

  (before-each
    (setq gitlab-host "GitLab host"
          gitlab-token-id "GitLab token ID"
          expected-response (make-request-response))
    (setf (request-response-status-code expected-response) 200)
    (spy-on 'request :and-return-value expected-response))

  (it "lists issues"
    (setf (request-response-data expected-response) '(((project_id . 1)
                                                       (title . "issue 1"))
                                                      ((project_id . 2)
                                                       (title . "issue 2"))))
    (let* ((page 1)
           (per-page 20)
           (issues (gitlab-list-issues page per-page)))
      (expect (length issues) :to-equal-or-exceed 0)
      (mapcar (lambda (i)
                (expect (assoc-default 'title i) :not :to-equal "")
                (expect (assoc-default 'project_id i) :to-be-a-number)
                (message "Issue: %s" (assoc-default 'title i)))
              issues)
      (expect 'request
              :to-have-been-called-with
              (concat gitlab-host "/api/v4/issues")
              :type "GET"
              :headers `(("PRIVATE-TOKEN" . ,gitlab-token-id)
                         ("connection" . "close"))
              :sync t
              :params `((page . ,(number-to-string page))
                        (per_page . ,(number-to-string per-page)))
              :parser 'json-read))))

What do you think?

chrycheng avatar Feb 16 '18 18:02 chrycheng