Mocking return result from GetAsync

Hi all,

I am trying to write some unit tests against my service which inject the IContentsClient. So I am using Moq to mock IContentsClient. However I am not able to stub or fake the result from GetAsync which is ContentsResult. I need to use the Data property to implement my business logic.

The problem is that Data property in ContentsResult is readonly so I am not able to stub and fake it.

Is there any other ways to write unit tests against GetAsync method using Moq.

Cheers

I am not a Moq user, but I have experience with FakeItEasy. The Data property is readonly but it always has a value, so you can easily do something like this.

class BlogData {
   public string Title { get; set; }
}

class BlogEntity : Content<BlogData> {}

var post = new BlogEntity();
post.Data.Title = "my-title";

var posts = new ContentResults<BlogEntity, BlogData>();
posts.Items.Add(post);
posts.Total = 10;

A.CallTo(() => contentsClient.GetAsync(...))
   .Returns(posts);

I don’t see the problem.

1 Like

Perfect. Thank you so much.

I am very new to Squidex so it is really help me.

Cheers

1 Like

Hi Sebatien,

Do you know how I can mock GetAllAsync in my unit test.

For example, this is my function

  async Task UpdateParentTopicRef(Topic topic)
                    {
                        if (lookUpParentRef.ContainsKey(topic.Data.TopicParentID ?? 0))
                        {
                            var updatedTopicData = new TopicData
                            {
                                TopicId = topic.Data.TopicId,
                                Name = topic.Data.Name,
                                Path = topic.Data.Path,
                                PathSlug = topic.Data.PathSlug,
                                TopicParentID = topic.Data.TopicParentID,
                                Parent = new List<string>
                                {
                                    lookUpParentRef[topic.Data.TopicParentID]
                                }
                            };
                            await topicSquidexClient.UpdateAsync(topic.Id, updatedTopicData);
                            var migrationResultId = new MigrationResultIds(topic.Data.TopicId.ToString(), topic.Id);
                            migrationResultIds.Add(migrationResultId);
                        }
                    }

                    await topicSquidexClient.GetAllAsync(async topic => { await UpdateParentTopicRef(topic); });

I dont know how to mock the UpdateParentTopicRef

Could you help me?

Many thanks

I am not a moq user (as I said).

But in FakeItEasy it works like this:

A.CallTo(() => contentsClient.GetAllAsync(A<Func<TEntity, Task>>.Ignored, ...))
   .Invokes(c =>
   {
     var callback = c.GetArgument<Func<TEntity, Task>>(0); // First parameter.

    callback(CreateMockContent()).Result; // await is not supported here.
    callback(CreateMockContent()).Result;
    callback(CreateMockContent()).Result;
    callback(CreateMockContent()).Result;
   });

Thanks for suggestions. I have managed to write tests successfully.

1 Like