0

I got a method like this one:

public async Task<List<MyEntity>> GetEntities()
        {
            var query = this.table.CreateQuery<DynamicTableEntity>()
                   .Where(d =>  d.Properties[nameof(MyEntity.Fid)].GuidValue == Guid.Empty)
                   .AsTableQuery();

            var result = table.ExecuteQuery<DynamicTableEntity>(query);

            ...
// Do more things here

        }

The thing is that when I run the unit test it crashes in this line "this.table.CreateQuery().Where". I think I need to mock the CreateQuery method so that it returns a prepared result that can be filtered, but I cannot find no information on how to mock the call to table.createQuery.

I have tried this:

cloudTable.Setup(s => s.CreateQuery<DynamicTableEntity>()).Returns(new TableQuery<DynamicTableEntity>());

Although that compiles, in runtime it gets me only an error:

System.NullReferenceException: 'Object reference not set to an instance of an object

I guess that in the "where" parts it must be trying to access to some sort of collection that it is still null. The question is how could I populate my fake object "new TableQuery()" with a few fake rows?

Or just, how to manually create a dummy TableQuery with values that I can use as a value to return in my mock?

Art
  • 163
  • 1
  • 8
  • You will have to return an instance of `DynamicTableEntity` that also has the `Where` and `AsTableQuery` methods mocked. – phuzi Sep 13 '22 at 07:51
  • Seems a good idea @phuzi, but could you provide an example of how to do that? Since DynamicTableEntity is not an abstraction I cannot mock it directly. I can create a mock of the interface that it implements ITableEntity like this: var mymock = new Mock(); but then if I try to do this: cloudTable.Setup(x => x.CreateQuery()).Returns(mymock); it does complain because DynamicTableEntity it is not a dynamic table query – Art Sep 19 '22 at 14:30

0 Answers0