I'm using the FakeClient by sigs.k8s.io/controller-runtime/pkg/client/fake
. In one of my unit tests, the Update()
method of FakeClient should fail to update the k8s object and return an error. How to achieve this without faking the Update()
method. Here is an example:
data.go
type Data struct {
dep *Deployment
}
func (d Data) updateDeployment(c client.Client) error {
return c.Update(context.Background(), dep)
}
test.go
type fakeData struct {
dep *Deployment
}
func TestUpdateDeploymentFailed(t *testing.T){
dep := &Deployment{}
fd := fakeData{
dep : dep,
}
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(dep).Build()
err := fd.updateDeployment(fakeClient)
assert.NotNil(t, err)
}
I want the fd.updateDeployment(fakeClient)
method to return an error without mocking the Update()
method of fake client.