5

We've been using sqlmock testing func() that are using select - no problems there.

It's not working with updates.

The func we want to test is:

func PutTag(tag *Tag) error {
...
    err = db.DB.Where("id=?", tag.Id).Save(tag).Error
...
}

We've defined the test func like this:

func (s *Suite) TestPutTag() {
    tag := Tag{Id: 2, Name: "Test Tag 2"}
    s.mock.ExpectBegin()
    s.mock.ExpectQuery("UPDATE `tags` SET `name` = ?  WHERE `tags`.`id` = ? AND ((id=?))").
        WithArgs(tag.Name, tag.Id, tag.Id)
    s.mock.ExpectCommit()
    err := PutTag(&tag)
    require.NoError(s.T(), err)
}

This is returning..

[2019-08-30 14:36:36]  call to ExecQuery 'UPDATE `tags` SET `name` = ?  WHERE `tags`.`id` = ? AND ((id=?))' with args [{Name: Ordinal:1 Value:Test Tag 2} {Name: Ordinal:2 Value:2} {Name: Ordinal:3 Value:2}], was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
  - matches sql: 'UPDATE `tags` SET `name` = ?  WHERE `tags`.`id` = ? AND ((id=?))'
  - is with arguments:
    0 - Test Tag 2
    1 - 2
    2 - 2

The sql statements seem the same to me.

robbieperry22
  • 1,753
  • 1
  • 18
  • 49
  • 1
    The error is pretty direct. What part of the error message is confusing you? You're expecting a Prepare, but your function isn't doing a prepare. – Jonathan Hall Aug 30 '19 at 10:39
  • 3
    oh, thank you, @Flimzy, i was trying all different things and none of them worked, so just got confused in the end... I've updated the question with ExpectQuery and respective output, still giving error, as far as i understood because of the arguments, are they suppose to be some particular kind of struct? – Maira Saliyeva Aug 30 '19 at 13:47
  • I have the same issue. For some reason for Update its completely ignoring the query that should occur. @MairaSaliyeva, were you able to resolve this? Its almost 2 years later. – ozn Aug 28 '21 at 04:19

0 Answers0