Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you unit test mixins in Dart?

I can find no documentation on this. The only way I see is to test a class that has the mixin added, but this is not a sound way of testing.

like image 762
CrenshawDK Avatar asked Oct 24 '25 04:10

CrenshawDK


1 Answers

/// example mixin
///
mixin TestMixin {
  String testing(String abc) {
    print("your function logic goes here");
    return abc;
  }
}

/// This is just a helper class to get all the [mixin] functions
///
class FunctionHoldingClassForMixin with TestMixin {}

Now you can use this in main function as shown below.

void main() {
  late FunctionHoldingClassForMixin mockClass;
  setUp(() {
    mockClass = FunctionHoldingClassForMixin();
  });

  test("mention what you wanna test", () {
    var result = mockClass.testing("testing abc");
    expect(result, "testing abc");
  });
}
  • Hope this resolves your equery 👨‍💻.
  • In case if someone finds a better approach do comment to improve the answer.
  • Thank you 😅
like image 98
dolar Avatar answered Oct 26 '25 17:10

dolar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!