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
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.
MinioClientimplements two interfaces,IBucketOperationsandIObjectOperations. It wasn't obvious. I had to clone their repo and look at the class to figure it out.IBucketOperationscontains theBucketExistsAsyncmethod. If that's the only method you need or if all the methods you need are defined inIBucketOperationsthen 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 -
IBucketOperationsandIObjectOperationsthen 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: