Sto correndo su 1.0.3 sul mio computer portatile Ubuntu 12.04.1 e mi sono imbattuto in un problema in cui se eseguo del codice in main(), si comporta molto più diversamente che se lo eseguissi con go test.Go Code si comporta in modo diverso in go test vs go run
Ecco il mio esempio:
Da main.go
package main
import (
"image"
"image/jpeg"
"fmt"
"myproj/htmlutil"
[some imports removed]
)
func main() {
img, err := htmlutil.GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
fmt.Println("There was a problem ",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
Da myproj/htmlutil_test.go
package htmlutil
import (
"image"
"fmt"
"testing"
[some imports removed]
)
func TestGetImageFromURL(t *testing.T){
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Fatalf("There was a problem %q",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
e la funzione che essi chiamano, GetResizedImageFromWeb(), è in MyProj/htmlutil .go:
package htmlutil
import (
"errors"
"fmt"
"image"
"io/ioutil"
"net/http"
[some imports removed]
)
func GetResizedImageFromWeb(imageURL string) (image.Image, error) {
resp, err := http.Get(imageURL)
if err != nil {
return nil, errors.New(fmt.Sprint("There was a problem reading the site %q Debug[%s]",imageURL, err))
}
defer resp.Body.Close()
//Decode the image using image's general purpose decoder
image, s, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return resizeImage(image), nil
}
Quando si esegue "go run main.go" dal riga di comando, vedo i limiti dell'immagine dall'url e posso salvarla come un file jpg sul disco se voglio usare una funzione in main.go. Tuttavia, quando si esegue "go test" dal pacchetto htmlutil, ottengo il seguente errore:
There was a problem "image: unknown format"
cosa sta causando il problema a fallire solo nei test di unità? Che cosa sto facendo di sbagliato?
La mia unica ipotesi è che per qualsiasi motivo, il html.Get() non restituisce tutti i dati nello scenario di test, ma sono ancora sconcertato sul motivo per cui ciò accade.