In my code I have various endpoints but they all have same controller code. I am using macros for this. Now i am writing test cases for it. Test cases are almost similar for each end point. like this
test "index/2 shows all the records" do
build_conn()
|> get(api_v1_company_path(build_conn(), :index))
|> json_response(200)
end
Now if i replace company with country in the ap1_v1_company_path. It will run for the company. But i have to wrote this all over again.
Is there any other solution I can do so that I should write minimum amount of code and it works for every endpoint?
Thanks
@spec api_test_route(module) :: (([term]) -> String.t())
defp api_test_route(controller_module) do
fun =
case controller_module do
MyApp.API.V1.CompanyController -> :api_v1_company_path
MyApp.API.V1.CountryController -> :api_v1_country_path
# etc
end
fn args -> apply(MyApp.Router.Helpers, fun, args) end
end
Usage
test "index/2 shows all the records" do
conn = build_conn()
path = api_test_route(MyApp.API.V1.CompanyController).([conn, :index])
conn
|> get(path)
|> json_response(200)
end
There is probably some helper function in phoenix to get path function names (like : api_v1_company_path) from controller module names (like MyApp.API.V1.CompanyController) to avoid the case statement in api_test_route/1.