Skip to content

Commit a876975

Browse files
johnstcnEmyrk
authored andcommitted
dbauthz: allow asserting errors in tests
1 parent 15e91ff commit a876975

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

coderd/database/dbauthz/setup_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ func (s *MethodTestSuite) Subtest(testCaseF func(db database.Store, check *expec
171171
fakeAuthorizer.AlwaysReturn = nil
172172

173173
outputs, err := callMethod(ctx)
174-
s.NoError(err, "method %q returned an error", methodName)
174+
if testCase.err == nil {
175+
s.NoError(err, "method %q returned an error", methodName)
176+
} else {
177+
s.EqualError(err, testCase.err.Error(), "method %q returned an unexpected error", methodName)
178+
}
175179

176180
// Some tests may not care about the outputs, so we only assert if
177181
// they are provided.
@@ -292,6 +296,7 @@ type expects struct {
292296
assertions []AssertRBAC
293297
// outputs is optional. Can assert non-error return values.
294298
outputs []reflect.Value
299+
err error
295300
}
296301

297302
// Asserts is required. Asserts the RBAC authorize calls that should be made.
@@ -316,6 +321,12 @@ func (m *expects) Returns(rets ...any) *expects {
316321
return m
317322
}
318323

324+
// Errors is optional. If it is never called, it will not be asserted.
325+
func (m *expects) Errors(err error) *expects {
326+
m.err = err
327+
return m
328+
}
329+
319330
// AssertRBAC contains the object and actions to be asserted.
320331
type AssertRBAC struct {
321332
Object rbac.Object

0 commit comments

Comments
 (0)