2012-12-30 24 views
6

Tôi đang chạy phiên bản 1.0.3 trên máy tính xách tay Ubuntu 12.04.1 của mình và tôi đã gặp sự cố khi tôi chạy một số mã trong main(), nó hoạt động nhiều khác hơn nếu tôi chạy nó với thử nghiệm.Mã Go hoạt động khác đi thử nghiệm khi chạy thử

Dưới đây là ví dụ của tôi:
Từ 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()) 
} 

Từ 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()) 
} 

và chức năng mà họ gọi, GetResizedImageFromWeb(), là trong 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 
} 

Khi tôi chạy "go run main.go" từ dòng lệnh, tôi thấy các giới hạn của hình ảnh từ url và có thể lưu nó dưới dạng tệp jpg trên đĩa nếu tôi muốn sử dụng hàm trong main.go. Tuy nhiên, khi tôi chạy "thử nghiệm" từ gói htmlutil, tôi nhận được lỗi sau:

There was a problem "image: unknown format" 

Điều gì gây ra sự cố chỉ thất bại trong các bài kiểm tra đơn vị? Tôi đang làm gì sai?

Đoán duy nhất của tôi là vì lý do nào, html.Get() không trả về tất cả dữ liệu trong kịch bản thử nghiệm, nhưng tôi vẫn bối rối là tại sao điều đó xảy ra.

Trả lời

2

Tôi đã cố gắng giải pháp rputikar của (sử dụng t.Fatal() thay vì fmt.Println()), nhưng điều đó không giúp đỡ.

I đã làm để ý rằng rputikar đang làm điều gì đó khác biệt một cách tinh tế với hàng nhập khẩu của anh ấy hơn tôi. nhập khẩu của tôi trong htmlutil.go trông giống như:

package htmlutil  

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

nhưng cả hai main.go tôi và main_test.go rputikar của chứa một nhập khẩu bổ sung, "image/jpeg". Vì vậy, tôi đã thêm vào danh sách nhập htmlutil.go của tôi và giải quyết được vấn đề. Tôi nghĩ tôi sẽ thêm "_ image/png""_ image/gif" chỉ để kiểm tra trong tương lai

4

Trong các thử nghiệm, bạn thực sự nên kiểm tra kết quả của các cuộc gọi chức năng của mình.

Kiểm tra chạy với/dev/null trên bảng điều khiển. Do đó các đầu ra fmt/log không hiển thị. Bạn nên làm điều gì đó như sau trong 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()) 
    } 

} 

Tôi chỉ sao chép mã của bạn như sau:

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

Đầu ra của kiểm tra đi

PASS 
ok  test 0.843s 

phiên bản đi của tôi là go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64

Các vấn đề liên quan