0

I have codes like this:

string bucketName = "wistest-dev";
           string fileNames = "/uploadFile/document/202112240201573/test111202112240202618.txt";
            var min = new Mock<MinioClient>();
            MinioClient minios = new MinioClient("test", "wistest", "wistestqas");
            var helper = new FileUpLoadHelper(minios, bucketName);
            FileUpLoadHelper.GetMinioClient();
            min.Setup(_ => _.BucketExistsAsync(bucketName, default)).Returns(BucketExistsAsync(bucketName, default));

but I got

Non-overridable members (here: MinioClient.BucketExistsAsync) may not be used in setup / verification expressions."

Any suggestions?

Thanks

MD. RAKIB HASAN
  • 3,670
  • 4
  • 22
  • 35

1 Answers1

1

You can't mock a non-virtual class method. This answer explains why.

Sometimes if a class doesn't implement an interface we have to create our own interface and class. The class adapts or "wraps" an instance of the other class, calling its methods. Instead of directly depending on the old class, we depend on our new interface because we can mock its methods.

In this case you won't need to. MinioClient implements two interfaces, IBucketOperations and IObjectOperations. It wasn't obvious. I had to clone their repo and look at the class to figure it out.

IBucketOperations contains the BucketExistsAsync method. If that's the only method you need or if all the methods you need are defined in IBucketOperations then you can replace your dependency on the class with a dependency on the interface, and then you're all set. You can mock the interface's method.

If you need operations from both interfaces - IBucketOperations and IObjectOperations then you can you can depend on both interfaces.

You can find more about both interfaces in your IDE or on GitHub.

It wasn't intuitive. There are files named BucketOperations.cs and ObjectOperations.cs, but they all contain partial definitions of MinioClient, like this:

public partial class MinioClient : IBucketOperations
Scott Hannen
  • 27,588
  • 3
  • 45
  • 62