From f75460ab6edf0728d81736643c4ddfc5041cfc69 Mon Sep 17 00:00:00 2001 From: Jake Huggart Date: Tue, 16 Jul 2019 13:49:17 -0500 Subject: [PATCH] Update BatchWriteItem to look through entire slice of expectations rather than only checking in order --- batch_write_item.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/batch_write_item.go b/batch_write_item.go index 2e4caae..baa6def 100644 --- a/batch_write_item.go +++ b/batch_write_item.go @@ -22,19 +22,15 @@ func (e *BatchWriteItemExpectation) WillReturns(res dynamodb.BatchWriteItemOutpu // BatchWriteItem - this func will be invoked when test running matching expectation with actual input func (e *MockDynamoDB) BatchWriteItem(input *dynamodb.BatchWriteItemInput) (*dynamodb.BatchWriteItemOutput, error) { if len(e.dynaMock.BatchWriteItemExpect) > 0 { - x := e.dynaMock.BatchWriteItemExpect[0] //get first element of expectation - - if x.input != nil { - if !reflect.DeepEqual(x.input, input.RequestItems) { - return nil, fmt.Errorf("Expect input %+v but found input %+v", x.input, input.RequestItems) + for i, x := range e.dynaMock.BatchWriteItemExpect { + if x.input != nil { + if reflect.DeepEqual(x.input, input.RequestItems) { + e.dynaMock.BatchWriteItemExpect = append(e.dynaMock.BatchWriteItemExpect[:i], e.dynaMock.BatchWriteItemExpect[i:]...) + return x.output, nil + } } } - - // delete first element of expectation - e.dynaMock.BatchWriteItemExpect = append(e.dynaMock.BatchWriteItemExpect[:0], e.dynaMock.BatchWriteItemExpect[1:]...) - - return x.output, nil } - return nil, fmt.Errorf("Batch Write Item Expectation Not Found") + return nil, fmt.Errorf("Batch Write Item Expectation Failed. Expected one of %+v to equal %+v", e.dynaMock.BatchWriteItemExpect, input.RequestItems) }