Unit test the Plumber function

I have written a plumber API and exposed as a service. Now I want to write down the unit test case for all the API's.

Right now API's are doing the below functionality

  1. Connecting to the database and getting the data
  2. Grouping the data
  3. Using the ggplot to plot the graph with the given data.

I have few issue end up with

  1. I was not able to mock the database connection.
  2. How do we call the API function from the testthat? API's doesn't have name to call from testthat function file.
  3. All the API has request and response object how do we mock that? when I tried look into the request and response object it is printing as environment.

Kindly do the needful.

You can get inspiration from how plumber uses testthat internally.

plumber/tests/testthat at main · rstudio/plumber (github.com)

plumber/test-routing.R at main · rstudio/plumber (github.com)

Thanks @meztez for commenting.

Code base has testing the plumber api endpoint, content, content-type, etc.

I was looking for the DB connection Mock up and query data mocking. I have used the RPostgres package to establish the connection.

Couldn't mock it, Tried with Mockery, Mockery2, Monad and fakeweb. It is not mocking the connectivity.

Either package is not available in CRAN or the functions are not exported.

Looking forward for the help with this.

@hariprasad What would you expect a database connection mock to do?

I'm not sure that I understand? I would replace with some actual query results for test purpose, probably stored in an .rds. file.

Thanks for replying @meztez . I have took the inspiration from the plumber test case and created it. Thanks for the help.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.