2012-12-30 53 views
6

मैं अपने उबंटू 12.04.1 लैपटॉप पर 1.0.3 चला रहा हूं और मैंने एक समस्या पर ठोकर खाई है जहां मैं मुख्य() में कुछ कोड चलाता हूं, यह बहुत व्यवहार करता है अगर मैं इसे परीक्षण के साथ चलाता हूं तो उससे अलग तरीके से।गो कोड अलग-अलग परीक्षण में बना रहता है

यहाँ मेरी उदाहरण है:
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()) 
} 

से 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()) 
} 

से और समारोह है कि वे कहते हैं, GetResizedImageFromWeb(), 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 
} 

जब मैं "go run main.go" चलाता हूं कमांड लाइन, मैं यूआरएल से छवि की सीमाएं देखता हूं और इसे डिस्क पर एक jpg फ़ाइल के रूप में सहेज सकता हूं अगर मैं main.go में किसी फ़ंक्शन का उपयोग करना चाहता हूं।

There was a problem "image: unknown format" 

क्या केवल इकाई परीक्षण में विफल समस्या का कारण है: हालांकि, जब मैं चलाने htmlutil पैकेज, मैं निम्नलिखित त्रुटि मिलती है से "परीक्षण जाना"? मैं क्या गलत कर रहा हूं?

मेरा एकमात्र अनुमान यह है कि किसी भी कारण से, html.Get() परीक्षण परिदृश्य में सभी डेटा वापस नहीं कर रहा है, लेकिन मैं अभी भी परेशान हूं कि ऐसा क्यों होता है।

उत्तर

2

मैं rputikar के समाधान का प्रयास किया है (t.Fatal (का उपयोग करें) fmt.Println() के बजाय), लेकिन यह मदद नहीं की।

I नोटिस किया कि rputikar मेरे आयात के साथ कुछ अलग तरीके से कुछ कर रहा था। htmlutil.go में मेरे आयात देखा की तरह:

package htmlutil  

import (
    "errors" 
    "fmt" 
    "image" 
    "io/ioutil" 
    [some imports removed] 
    "net/http" 
) 

लेकिन दोनों मेरे main.go और rputikar के main_test.go एक अतिरिक्त आयात, "image/jpeg" निहित। इसलिए, मैंने अपनी htmlutil.go आयात सूची में जोड़ा और समस्या को हल किया। मुझे लगता है कि मैं "_ image/png" और "_ image/gif" केवल भविष्य के प्रूफिंग

के लिए जोड़ दूंगा
4

परीक्षणों में आपको वास्तव में अपने फ़ंक्शन कॉल के परिणामों की जांच करनी चाहिए।

टेस्ट कंसोल पर/dev/null के साथ चलते हैं। इसलिए एफएमटी/लॉग आउटपुट दिखाई नहीं दे रहे हैं। आप htmlutil_test.go में निम्नलिखित की तरह कुछ करना चाहिए

func TestMain(t *testing.T) { 
    img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg") 
    if err != nil { 
     t.Error("There was a problem ", err) 
    } 

    bounds := image.Rectangle{ 
     image.Point{0, 0}, 
     image.Point{616, 462}} 

    if img.Bounds() != bounds { 
     t.Error("Incorrect Bounds were ", img.Bounds()) 
    } 

} 

इस प्रकार मैं सिर्फ अपने कोड की नकल की:

main.go

package main 

import (
    "errors" 
    "fmt" 
    "image" 
    _ "image/jpeg" 
    "net/http" 
) 

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, _, err := image.Decode(resp.Body) 
    if err != nil { 
     return nil, err 
    } 

    return image, nil 
} 

func main() { 
    img, err := 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()) 
} 

main_test.go

package main 

import (
    "image" 
    "log" 
    "testing" 
) 

func TestMain(t *testing.T) { 
    img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg") 
    if err != nil { 
     t.Error("There was a problem ", err) 
    } 

    bounds := image.Rectangle{ 
     image.Point{0, 0}, 
     image.Point{616, 462}} 

    if img.Bounds() != bounds { 
     t.Error("Incorrect Bounds were ", img.Bounds()) 
    } 
} 

जाने का आउटपुट

PASS 
ok  test 0.843s 

मेरे जाने संस्करण go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64