Skip to content

Instantly share code, notes, and snippets.

View csmith0651's full-sized avatar

Craig Smith csmith0651

  • Arlington, MA
View GitHub Profile
@csmith0651
csmith0651 / mock_controller.go
Last active October 17, 2016 20:12
example of a creating a mock controller for you test suite.
var _ = Describe("Person", func() {
var (
mockFetcher *mock_S3Fetcher.MockS3Fetcher
ctrl *gomock.Controller
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
})
AfterEach(func() {
ctrl.Finish()
@csmith0651
csmith0651 / mock_testcall.go
Last active October 17, 2016 20:17
example of registering a mock call on the mock object.
Context("ReadPersonFromS3 -- using mock", func() {
bucket := "testbucket"
key := "persondata.txt"
objectInput := &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
}
goodObjectOutput := &s3.GetObjectOutput{
Body: S3Fetcher.NewMockBody([]byte(goodJson)),
}
@csmith0651
csmith0651 / test_error.go
Created October 18, 2016 12:15
prime a mock call to return an error and test for it
Context("simulate an error retrieving data from S3", func() {
var testCall *gomock.Call
BeforeEach(func() {
testCall = mockFetcher.EXPECT().GetObject(gomock.Any()).Return(nil, fmt.Errorf("AWS error")).AnyTimes()
})
It("and fails", func() {
_, err := ReadPersonFromS3(mockFetcher)
Expect(err).To(HaveOccurred())
testCall.Times(1)
})