When testing the return value of method A, we want to mock the return value of method B in method A.

Asked 1 years ago, Updated 1 years ago, 82 views

flutter dart
mockito


What you want to do and what you can't do

Currently, when testing the return value of method A, we are trying to mock the return value of method B in method A.
For the time being, if the return value of method B executed in method A is used as an argument, it can be tested without being mocked, but I don't want to do this solution because it changes the existing code.

//sample.dart
US>class Sample {
  inta(){
    return b()+4;
  }

  intb(){
    return3;
    // Usually seven.
    // If it's mocking test.
  }
}
//sample_test.dart

import 'package: sellca_pad_3/models/sample.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import'sample_test.mocks.dart';

@GenerateMocks ([Sample])
void main() {
  test('Test Mock',(){
    final mockSample=MockSample();
    when(mockSample.b()) .thenReturn(5);
    final sample=Sample();
    final addResult=sample.a();
    expect(addResult, 9);
  });
}

Expectations and Actual Values for the Results of this Test

Expected value: 9
Actual value: 7

In when(mockSample.b()) .thenReturn(5); I wanted to change the return value of method b from 3 to 5, so that the return value of method a is from method b's return value (3)+4 to method b's mocked return value (5)+4 to 9, but the unmocked method b is returned in method a.
If you have any advice to solve this problem, please give me some advice.

flutter dart mockito

2022-09-30 19:26

1 Answers

I think the code I gave you is incomplete, but I put the mock function in the code I gave you, but I don't use it.

Very simple, I created a new instance of Sample with this code, which has nothing to do with the mock.

final sample=Sample();


2022-09-30 19:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.