You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have started using the library for a pet project of mine and I am really excited about it. It removes a lot of the boilerplate that I need to write for anything related to databases. However, I had some issues when I tried to unit test some my code.
To be more precise, I use the qrm.DB interface to pass it to any Query made from jet and mock it for the unit tests, but I couldn't find a way to mok it work properly. When jet calls Query it's seems that internally calls the QueryContext method of the DB interface which has the following signature QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error).
It may either return a sql.Rows pointer or an err, but sql.Rows is a struct that doesn't expose any of its internal fields and I cannot pass anyting else except a nil pointer, which makes QueryContext to panic and propagates to jet Query method.
Can you suggest a solution or an alternative solution to this issue?
The text was updated successfully, but these errors were encountered:
MarioSimou
changed the title
How to properly mock qrm.DB so that it works with jet Query method
How to properly mock qrm.DB so that it works with jet Query method
Apr 25, 2020
My suggestion would be not to mock the database. It adds unnecessary complexity to the project, for very little or no gain at all. Also every test that mocks database can be written without mock.
For instance:
funcSomeServiceMethod(db, ...) {
...customer->querydbtogetaccount...// business logicorders->querydbtogetorders...// business logic
}
can be replaced with:
funcSomeServiceMethod(db, ...) {
data:=struct{
model.CustomerOrders []model.Orders
}
someStatement.Query(db, data)
bussinesLogic(data)
}
funcbussinesLogic(data) {
...// business logic...// business logic
}
bussinesLogic - is tested with black box unit tests
SomeServiceMethod - is tested with service tests(tests with real db connection)
Oh, I see the point behind that but that would need to refactor a bit the code. However, I tested sqlmock with go-jet and it works fine. So you can proceed closing this issue.
Hello,
I have started using the library for a pet project of mine and I am really excited about it. It removes a lot of the boilerplate that I need to write for anything related to databases. However, I had some issues when I tried to unit test some my code.
To be more precise, I use the
qrm.DB
interface to pass it to anyQuery
made from jet and mock it for the unit tests, but I couldn't find a way to mok it work properly. When jet callsQuery
it's seems that internally calls theQueryContext
method of theDB
interface which has the following signatureQueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
.It may either return a
sql.Rows
pointer or anerr
, but sql.Rows is a struct that doesn't expose any of its internal fields and I cannot pass anyting else except a nil pointer, which makes QueryContext to panic and propagates to jetQuery
method.Can you suggest a solution or an alternative solution to this issue?
The text was updated successfully, but these errors were encountered: