@@ -15,20 +15,31 @@ import (
1515
1616func TestCurrentWeather (t * testing.T ) {
1717 testCases := map [string ]struct {
18- arguments map [string ]any
19- errString string
20- wait string
18+ fillWeatherService func (mocksWeather * mocks.MockWeatherService )
19+ arguments map [string ]any
20+ errString string
21+ wait string
2122 }{
2223 "empty_city" : {
2324 wait : "city must be a string" ,
2425 },
2526 "city_not_found" : {
27+ fillWeatherService : func (mocksWeather * mocks.MockWeatherService ) {
28+ mocksWeather .EXPECT ().
29+ Current (context .Background (), "Tokyo" ).
30+ Return ("" , errors .New ("weather API not available. Code: 400" ))
31+ },
2632 arguments : map [string ]any {
2733 "city" : "Tokyo" ,
2834 },
2935 errString : "weather API not available. Code: 400" ,
3036 },
3137 "successful_request" : {
38+ fillWeatherService : func (mocksWeather * mocks.MockWeatherService ) {
39+ mocksWeather .EXPECT ().
40+ Current (context .Background (), "London" ).
41+ Return ("<h1>London weather data</h1>" , nil )
42+ },
3243 arguments : map [string ]any {
3344 "city" : "London" ,
3445 },
@@ -40,31 +51,21 @@ func TestCurrentWeather(t *testing.T) {
4051 defer ctrl .Finish ()
4152
4253 mocksWeather := mocks .NewMockWeatherService (ctrl )
43- mocksWeather .EXPECT ().
44- Current (context .Background (), "Tokyo" ).
45- Return ("" , errors .New ("weather API not available. Code: 400" ))
46- mocksWeather .EXPECT ().
47- Current (context .Background (), "London" ).
48- Return ("<h1>London weather data</h1>" , nil )
54+
4955 svc := mocks .NewMockServices (ctrl )
50- svc .EXPECT ().Weather ().Return (mocksWeather ).Times ( 2 )
56+ svc .EXPECT ().Weather ().Return (mocksWeather ).AnyTimes ( )
5157
5258 handler := CurrentWeather (svc )
5359
5460 for name , tc := range testCases {
5561 t .Run (name , func (t * testing.T ) {
56- request := mcp.CallToolRequest {
57- Params : struct {
58- Name string `json:"name"`
59- Arguments map [string ]any `json:"arguments,omitempty"`
60- Meta * struct {
61- ProgressToken mcp.ProgressToken `json:"progressToken,omitempty"`
62- } `json:"_meta,omitempty"`
63- }{
64- Arguments : tc .arguments ,
65- },
62+ if tc .fillWeatherService != nil {
63+ tc .fillWeatherService (mocksWeather )
6664 }
6765
66+ var request mcp.CallToolRequest
67+ request .Params .Arguments = tc .arguments
68+
6869 result , err := handler (context .Background (), request )
6970 if err != nil {
7071 assert .EqualError (t , err , tc .errString )
0 commit comments