1# Guide to write tests for resolver file
2
3## Getting started:
4- Well hello there! You have wondered into this file, so it means you must have written your first resolver file.
5- A good practice is whenever you write any function, you should write test cases for it, to see where in the code can be improved.
6- Don't forget to add mock gen rule to services
7- After adding your resolver function, runs `just generate-mocks` or `just update-mocks` for any new or changes to your resolver functions.
8
9## Writing test:
10
11- If you plan to write multiple tests using similar variables, I recommend declaring them at the beginning, so you don't have to keep initializing new variables. Here's an example:
12
13```
14var {
15 testString = "test-string"
16 testInt = 123
17 testCustomerID = "test-customer-id-1"
18}
19```
20
21- You will name your test similar to the function you want to test. For ex, if I have a function `GetIceCreams`, my test function name for it should be `TestGetIceCreams`
22- In your resolver test file, create a query/mutation function to grab the graphql query/mutation you want. I will use the `TestLogin` as an example to test `Login` mutation on graphql.
23
24```
25func TestLogin(t *testing.T) {
26 mock := gomock.NewController(t)
27 service := mocks.NewMockUserManagementService(mock)
28 service.EXPECT().
29 Login(gomock.Any(), "test", "password", "test_prefix-organization").
30 Return(&model.AuthPayload{}, nil).Times(1)
31 service.EXPECT().
32 Login(gomock.Any(), "test", "password", "more/paths/hello/test_prefix-organization").
33 Return(&model.AuthPayload{}, nil).Times(1)
34 c := &types.Config{BSP: bsltypes.BSPConfig{OrganizationPrefix: "test_prefix-"}}
35 r := &Resolver{UserManagementService: service, Config: c}
36 _, err := r.Mutation().Login(context.Background(), "test", "password", "/organization/")
37 assert.NoError(t, err)
38 _, err = r.Mutation().Login(context.Background(), "test", "password", "/more/paths/hello/organization/")
39 assert.NoError(t, err)
40}
41```
42
43- Setup up your `mock` and create mock user management `service`
44
45```
46mock := gomock.NewController(t)
47service := mocks.NewMockUserManagementService(mock)
48```
49
50- Set up `EXPECT()` to return what output you want from `Login`
51
52```
53service.EXPECT().
54 Login(gomock.Any(), "test", "password", "test_prefix-organization").
55 Return(&model.AuthPayload{}, nil).Times(1)
56```
57
58- Call `Login` and pass in its required parameters that you can either find the function or go to graphql (via dev1 site or running local bff + auth-proxy).
59- For `Login`, its required parameters are `username`, `password` and `organization`, so you pass your testing variables in.
60- The function will return `AuthPayload` and `error`, and we want it to return good output, so we set it to return our payload and set error to `nil`
61- `Time(1)` sets number of times a function call is expected to be executed, in this case it is `1`
62
63- Verify test org and services
64
65```
66c := &types.Config{BSP: bsltypes.BSPConfig{OrganizationPrefix: "test_prefix-"}}
67r := &Resolver{UserManagementService: service, Config: c}
68```
69
70- Finally, we add a comparison with our end result to see if the mock we setup above will return the same
71
72```
73_, err := r.Mutation().Login(context.Background(), "test", "password", "/organization/")
74 assert.NoError(t, err)
75```
76
77- If there is no error returns, it means the test passes.
78- But if there is error coming back, which means either test you setup is wrong or your `Login` function not returning the right output and you should go back and check it. Using Debugging tool in this proccess will speed up your trouleshoot :)
79
80
81
82### If there is any question or confusion on this guide, you can use some other resolver tests or reach out to the Platform team by tagging `edge-api` or post in `#ncr-edge-api` for more guidance. Thank you! :D
View as plain text