| 1 | package gostub |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "io/ioutil" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | const productionData = "production data" |
| 12 | |
| 13 | var dataReader io.Reader = bytes.NewReader([]byte(productionData)) |
| 14 | |
| 15 | func TestAssignableStub(t *testing.T) { |
| 16 | const testData = "test data" |
| 17 | stubs := Stub(&dataReader, strings.NewReader(testData)) |
| 18 | defer stubs.Reset() |
| 19 | |
| 20 | got, err := ioutil.ReadAll(dataReader) |
| 21 | if err != nil { |
| 22 | t.Fatalf("ReadAll failed: %v", err) |
| 23 | } |
| 24 | |
| 25 | if !bytes.Equal(got, []byte(testData)) { |
| 26 | t.Errorf("Got unexpected data:\n got %v\n want %v", string(got), string(testData)) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestUnassignableStub(t *testing.T) { |
| 31 | defer expectPanic(t, "Stub wrong type", "not assignable") |
| 32 | var noInterface interface{} = "test" |
| 33 | Stub(&dataReader, noInterface) |
| 34 | } |