Closed andre-fajar-n closed 5 months ago
Currently Transaction
can not be mocked, but you can try this way
func YourFunc() {
// other logic
r.enforcer.GetAdapter().(*gormadapter.Adapter).Transaction(r.enforcer, TransactionFunc("domainName"))
// other logic
}
func TransactionFunc(domainName string) func(i casbin.IEnforcer) error {
return func(i casbin.IEnforcer) error {
allPolicies := i.GetFilteredPolicy(1, domainName)
ok, err := i.RemovePolicies(allPolicies)
if err != nil {
return err
}
if !ok {
err := fmt.Errorf("cannot delete this rbac config")
return err
}
allGroupPolicies := i.GetFilteredGroupingPolicy(2, domainName)
ok, err := i.RemoveGroupingPolicies(allGroupPolicies)
if err != nil {
return err
}
if !ok {
err := fmt.Errorf("cannot delete this grouping policies")
return err
}
return nil
}
}
func Test_TransactionFunc(t *testing.T) {
// other logic
err := TransactionFunc(domainName)(enforcer)
// other logic
}
@andre-fajar-n closed as resolved
I have used transaction like this
Then I want to write unit test of this function, but I don't know how to mock the Transaction. Any solution for my problem?