Skip to content

Commit

Permalink
Merge pull request #2 from omarkhater-school/fix-chat-service-test
Browse files Browse the repository at this point in the history
Fix chat service test
  • Loading branch information
omarkhater-school authored Oct 15, 2024
2 parents 1e7790a + 93811ac commit 242e70a
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions spec/controllers/chat_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
require 'rails_helper'

RSpec.describe ChatController, type: :controller do
describe "POST#chat" do
it 'sends a new message to the AI chat bot' do
post :chat, params: {user_input: "Hello"}
end
describe "POST#chat" do
it 'sends a new message to the AI chat bot and validates the response body' do
# Mock the ChatService to avoid making actual API requests
chat_service = instance_double("ChatService")

# Mocking the response that would come from the API, simulating a valid response structure
# even if the content might reflect an invalid API key or other errors
allow(chat_service).to receive(:call).and_return({
"choices" => [
{
"message" => {
"content" => "Invalid API key"
}
}
]
})

# Trigger the controller action with a valid user_input
post :chat, params: { user_input: "Hello" }

# Parse the response body (you can check the format of the response, e.g., JSON)
parsed_response = JSON.parse(response.body)

# Validate that the response has the expected structure, without checking content
expect(parsed_response).to have_key("choices")
expect(parsed_response["choices"].first).to have_key("message")
expect(parsed_response["choices"].first["message"]).to have_key("content")

# Additionally, check that the HTTP status is successful (200)
expect(response).to have_http_status(:success)
end
end
end
end

0 comments on commit 242e70a

Please sign in to comment.