how to unit tests methods that contain calls to CreateQuery<DynamicTableEntity>()

172 Views Asked by At

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?

0

There are 0 best solutions below