Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FormRecognizer] Implement Model Factory for mocking support #14221

Merged
merged 31 commits into from
Aug 17, 2020

Conversation

kinelski
Copy link
Member

Fixes #11529.

@kinelski kinelski added Client This issue points to a problem in the data-plane of the library. Cognitive - Form Recognizer labels Aug 13, 2020
@kinelski kinelski self-assigned this Aug 13, 2020
@kinelski kinelski marked this pull request as ready for review August 13, 2020 23:28
Copy link
Member

@maririos maririos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment related to separate the ModelFactory for Training (and having it in the Training namespace)

@maririos
Copy link
Member

FYI @lenniebriscoe

@lenniebriscoe
Copy link

lenniebriscoe commented Aug 14, 2020

FYI @lenniebriscoe

Many thanks for working on this. It will definitely improve testability. A few questions though.

  1. I'm currently having to mock the FormRecognizerClient as below in order to work with the .StartRecognizeCustomFormsFromUriAsync() and .WaitForCompletionAsync(). Will there be a better way of doing this?
mockRecognizeCustomFormsOperation = new Mock<RecognizeCustomFormsOperation>("123/analyzeresults/123", mockFormRecognizerClient.Object);

 mockFormRecognizerClient
                .Setup(x => x.StartRecognizeCustomFormsFromUriAsync(It.IsAny<string>(),
                    It.IsAny<Uri>(), It.IsAny<RecognizeOptions>(),
                    It.IsAny<CancellationToken>()))
                .ReturnsAsync(mockRecognizeCustomFormsOperation.Object);

var responseBuilder = new ResponseBuilder().WithKeyValuePair("mapping_number", "10");

mockRecognizeCustomFormsOperation.Setup(x => x.WaitForCompletionAsync(It.IsAny<CancellationToken>()))
                .ReturnsAsync(responseBuilder.Build());

  1. .WaitForCompletionAsync returns a Task<Azure.Response>. Currently, I have created a fake and am inheriting from Response. Again is there a better way of doing this?

@kinelski
Copy link
Member Author

Hello, @lenniebriscoe.

Answering both of your questions: your approach is correct. That's how we designed mocking support in Azure SDK .NET libraries: service methods in clients/operations must be overridden, and model types can be instantiated by a static Model Factory. This is a guideline we follow in multiple libraries, so it's not likely to change soon.

Feel free to reach out if you have more questions!

@kinelski kinelski merged commit 3b69fc2 into Azure:master Aug 17, 2020
@kinelski kinelski deleted the fr-factory branch August 17, 2020 21:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Client This issue points to a problem in the data-plane of the library. Cognitive - Form Recognizer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Test] Provide Model Factory per .NET Mocking Guidelines
3 participants