<p>I'm using client-go (the k8s client for go) to programmatically retrieve and update some secrets from my cluster. While doing this, I'm facing the need of unit-testing my code, and after some investigation I stumbled upon client-go's <code>fake</code> client. However, I haven't been able to mock errors yet. I've followed the instructions from <a href="https://github.com/kubernetes/client-go/issues/742#issuecomment-633676780" rel="nofollow noreferrer">this issue</a>, but without any success.</p>
<p>Here you have my business logic:</p>
<pre class="lang-golang prettyprint-override"><code>func (g goClientRefresher) RefreshNamespace(ctx context.Context, namespace string) (err error, warnings bool) {
    client := g.kubeClient.CoreV1().Secrets(namespace)
    secrets, err := client.List(ctx, metav1.ListOptions{LabelSelector: &quot;mutated-by=confidant&quot;})
    if err != nil {
        return fmt.Errorf(&quot;unable to fetch secrets from cluster: %w&quot;, err), false
    }

    for _, secret := range secrets.Items {
        // business logic here
    }
    return nil, warnings
}

</code></pre>
<p>And the test:</p>
<pre class="lang-golang prettyprint-override"><code>func TestWhenItsNotPossibleToFetchTheSecrets_ThenAnErrorIsReturned(t *testing.T) {
    kubeClient := getKubeClient()
    kubeClient.CoreV1().(*fakecorev1.FakeCoreV1).
        PrependReactor(&quot;list&quot;, &quot;secret&quot;, func(action testingk8s.Action) (handled bool, ret runtime.Object, err error) {
            return true, &amp;v1.SecretList{}, errors.New(&quot;error listing secrets&quot;)
        })
    r := getRefresher(kubeClient)


    err, warnings := r.RefreshNamespace(context.Background(), &quot;target-ns&quot;)


    require.Error(t, err, &quot;an error should have been raised&quot;)
}
</code></pre>
<p>However, when I run the test I'm getting a <code>nil</code> error. Am I doing something wrong?</p>

<p>I've finally found the error... it is in the resource name of the reactor function, I had <code>secret</code> and it should be the plural <code>secrets</code> instead... :facepalm:. So this is the correct version of the code:</p>
<pre class="lang-golang prettyprint-override"><code>func TestWhenItsNotPossibleToFetchTheSecrets_ThenAnErrorIsReturned(t *testing.T) {
    kubeClient := getKubeClient()
    kubeClient.CoreV1().(*fakecorev1.FakeCoreV1).
        PrependReactor(&quot;list&quot;, &quot;secrets&quot;, func(action testingk8s.Action) (handled bool, ret runtime.Object, err error) {
            return true, &amp;v1.SecretList{}, errors.New(&quot;error listing secrets&quot;)
        })
    // ...
}
</code></pre>
