How to test Random method of dart library in Flutter?

207 Views Asked by At

I am trying to test the Random method from the dart library in Flutter. I got to know about seeding for the Random method to test it but it doesn't make the method purely random as it would behave without seeding because the seeded method always gives the same sequence of numbers. Is it any way to mock the method I tried mocking it and the mocked method never gets called as the method does not have the dependency passed as a parameter. The code I tried.

bacground_color.dart

 ///Method responsible for change of colour of background.
  Color? colorChanger() {
    const parameterRandom = 100;
    const parameterNextInt = 256;
    int parameterARGB = 0;
    final parameterARGB2 = Random().nextInt(parameterNextInt);
    final parameterARGB3 = Random().nextInt(parameterNextInt);
    final parameterARGB4 = Random().nextInt(parameterNextInt);

    if (background ==
        Color.fromARGB(
          parameterARGB,
          parameterARGB,
          parameterARGB,
          parameterARGB,
        ))
    //Case created done for the widget testing so that 1st time the color is,
    // what is specified in widget test and the second time it is all random
    {
      background = Color.fromARGB(
        Random(parameterRandom).nextInt(parameterNextInt),
        Random(parameterRandom).nextInt(parameterNextInt),
        Random(parameterRandom).nextInt(parameterNextInt),
        Random(parameterRandom).nextInt(parameterNextInt),
      );
    } else {
      parameterARGB = Random().nextInt(parameterNextInt);
      background = Color.fromARGB(
        parameterARGB,
        parameterARGB2,
        parameterARGB3,
        parameterARGB4,
      );
    }

    return background;
  }

main_file_unit_test.dart

import 'dart:math';
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:solid_software/Model/background_color.dart';

class MockRandom extends Mock implements Random {}

void main() {
  late MockRandom mockRandom;
  setUp(() {
    mockRandom = MockRandom();
  });
  test("Unit test of main method", () {
    const int nextIntParameter = 256;
    const int aRGBParameter = 74;
    when(() => mockRandom.nextInt(nextIntParameter)).thenReturn(aRGBParameter);
// final _MyHomePageState testOfColorChanger =const MyHomePage();
// testOfColorChanger
    final BackgroundColor backgroundColor = BackgroundColor();
    expect(backgroundColor.background, const Color.fromARGB(0, 0, 0, 0));
    backgroundColor.colorChanger();
    expect(
      backgroundColor.background,
      const Color.fromARGB(
          aRGBParameter, aRGBParameter, aRGBParameter, aRGBParameter),
    );
  });
}

I got some help asking around about seeding but not able to understand exactly how we can do this. The whole code is from the repository https://github.com/theashggl/solid_software

1

There are 1 best solutions below

1
marcinj On

You should inject your mocked random class instance into BackgroundColor instance. For example, change your class to:

class BackgroundColor {
  BackgroundColor({Random? random}) : _random = random ?? Random();

  final Random _random;
  //...

and use random like you were using Random() previously, ex.:

final parameterARGB2 = _random.nextInt(parameterNextInt);

Then, in you test code, inject mocked random as follows:

final BackgroundColor backgroundColor = BackgroundColor(random: mockRandom);