We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I have used transaction like this
err := r.enforcer.GetAdapter().(*gormadapter.Adapter).Transaction(r.enforcer, 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 := enforcer.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 })
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?
The text was updated successfully, but these errors were encountered:
Currently Transaction can not be mocked, but you can try this way
Transaction
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 }
Sorry, something went wrong.
@andre-fajar-n closed as resolved
hsluoyz
No branches or pull requests
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?
The text was updated successfully, but these errors were encountered: