0

I am using mock library in my .Net unit test and getting an error

cannot be accessed with an instance reference instead use type name.

I am getting this error at following line in my test method where it is calling cq.Instance. I am new to mock library. Could somebody let me know how do I call the static method?

attributeValue.Setup(cq => cq.Instance().CallQueryAsync(request, 1)).Returns(attrValue);

Actual method to be tested

 public static async Task<AttributeValueList> GetAttributeSecDateValueList(int attrId)
        {
            try
            {
                var request = AttributeValue.ResolveRequest(attrId);
                var response = await AsyncProxy<AttributeValue>.Instance().CallQueryAsync(request, (int)AttributeValue.OperationType.GetSecDateValues);

                var coll = new AttributeValueList();
                coll.AddRange(response);

                return coll;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

Proxy class

public class AsyncProxy<RT> : IDisposable
    where RT : class, new()
  {
    readonly WebServiceProxy<RT> _wsProxy;

    private AsyncProxy(WebServiceProxy<RT> webServiceProxy)
    {
      _wsProxy = webServiceProxy;
    }

    public static async Task<IEnumerable<RT>> Load(object parameters)
    {

      return await Instance().CallQueryAsync(parameters);
    }

    public static AsyncProxy<RT> Instance()
    {
      return new AsyncProxy<RT>(WebServiceProxy<RT>.Instance());
    }

    

    /// <summary>
    /// Return result set of Poco as smartCollection
    /// </summary>
    public async Task<SmartCollection<RT>> CallQueryAsync(object request, int? uniqueIdentifier = null, bool isLongRunning = false, [CallerMemberName]string memberName = "")
    {
//#if DEBUG
//      var stopwatch = new Stopwatch();
//      stopwatch.Start();
//#endif
      try
      {
        // We want to get rid of the proxy as soon as we are done with it
        using (_wsProxy)
        {
          var awaited = await _wsProxy.CallQueryAsync(request, uniqueIdentifier, isLongRunning);
          if (awaited == null)
            return null;

          var observableCollection = new SmartCollection<RT>();

          foreach (var item in awaited)
            observableCollection.Add(item as RT);

          return observableCollection;
        }
      }
      finally
      {
        Dispose();

//#if DEBUG
//        stopwatch.Stop();

//        Debug.WriteLine(null);
//        Debug.WriteLine($"****>>>>> AsyncProxy {memberName} took {stopwatch.ElapsedMilliseconds} ms <<<<<<<<****");
//#endif
      }
    }
}

test method

[TestMethod]
public void test()
{
        
    Task<SmartCollection<AttributeValue>> attrValue = null;
    var request = new AttributeValue();

    var attributeValue = new Mock<AsyncProxy<AttributeValue>>();

    attributeValue.Setup(cq => cq.Instance().CallQueryAsync(request, 1)).Returns(attrValue);
             
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Tom
  • 8,175
  • 41
  • 136
  • 267
  • Does this answer your question? [How to mock static methods in c# using MOQ framework?](https://stackoverflow.com/questions/12580015/how-to-mock-static-methods-in-c-sharp-using-moq-framework) – Peter Csala Jan 12 '23 at 06:41

0 Answers0