How can the OpenAIClient be mocked using MOQ to return a Completions response? I have a json response and would like to deserialize that to Response<Completions>
.
private OpenAIClient client = // initialize;
private async Task<Response<Completions>> Complete(string model, string prompt) =>
await client.GetCompletionsAsync(model, prompt);
The below attempt to return a response failed as the Completions class does not contain a constructor that can be used to deserialize the json response -
private readonly Mock<OpenAIClient> client= new();
client
.Setup(x => x.GetCompletionsAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Response.FromValue(JsonConvert.DeserializeObject<Completions>(completionsJson), Mock.Of<Response>()));
The Completions class constructor is marked as internal
and so are all the objects within the Completions class.
I know this is an old question, but I had the same issue and solved it by creating a Frankenstein object composed of a mix of Substitutes/Mocks (I am using NSubstitute
) alongside real objects.
const string userPrompt = "test";
const string responsePrompt = "response";
// real completion
var completion = OpenAIChatModelFactory.ChatCompletion(
role: ChatMessageRole.User,
content: new ChatMessageContent(ChatMessageContentPart.CreateTextPart(responsePrompt)));
// mocked PipelineResponse
var pipelineResponse = Substitute.For<PipelineResponse>();
// real ClientResult
var completionResponse = ClientResult<ChatCompletion>.FromValue(completion, pipelineResponse);
// mocking my final result
_chatClientMock.CompleteChatAsync(Arg.Any<List<ChatMessage>>(), Arg.Any<ChatCompletionOptions>(), Arg.Any<CancellationToken>())
.Returns(completionResponse);
// call your test service here that invokes _chatClientMock.CompleteChatAsync
// assertions...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With