dart-backend/angel

NoSuchMethodError: The method 'describeMismatch' was called on null.

Closed this issue · 2 comments

I'm trying to write some tests for my Rest API, and I'm getting a strange error when I try to test to fail with different values ​​than expected

PS C:\MyDartProjects\new_sali\backend> dart test .\test\incluir_processo_test.dart --chain-stack-traces
00:02 +0: incluir processo
Load configuration
DBLayer@connect localhost
Connection@disconnect
DBLayer@connect localhost
Connection@disconnect
DBLayer@connect localhost
Connection@disconnect
isEquals value: 2023 == 2023
isEquals value: 169 == 169
isEquals value: 1 == 1
isEquals value: 140050 == 140050
isEquals value: 140050 == 14005025
00:03 +0 -1: incluir processo [E]
  NoSuchMethodError: The method 'describeMismatch' was called on null.
  Receiver: null
  Tried calling: describeMismatch(Instance of 'Response', Instance of 'StringDescription', null, false)
  package:matcher                       expect
  test\incluir_processo_test.dart 76:5  main.<fn>


To run this test again: C:\tools\dart-sdk-2.18.7\bin\dart.exe test test/incluir_processo_test.dart -p vm --plain-name "incluir processo"
00:03 +0 -1: Some tests failed.
PS C:\MyDartProjects\new_sali\backend> 
import 'dart:convert';
import 'package:angel3_test/angel3_test.dart';
import 'package:angel3_validate/angel3_validate.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:new_sali_backend/src/shared/bootstrap.dart';
import 'package:test/test.dart';
import 'package:angel3_container/mirrors.dart';
import 'setup.dart';

Matcher isEquals(expectVal) {
  return predicate((dynamic value) {
    print('isEquals value: $value == $expectVal');
    return value == expectVal;
  }, 'is Equals');
}

void main() {
  late Angel app;
  late TestClient client;

  setUp(() async {
    app = Angel(reflector: MirrorsReflector());
    await app.configure(configureServer);
    client = await connectTo(app);
    await authenticate(client);
  });

  tearDown(() async {
    await client.close();
  });

  test('incluir processo', () async {
    final res = await client.post(Uri.parse('$baseUrl/protocolo/processos'),
        body: jsonEncode({
          'cod_processo': -1,
          'ano_exercicio': '2023',
          'cod_classificacao': 169,
          'cod_assunto': 1,
          'numcgm': 140050,
          'cod_usuario': 140050,
          'cod_situacao': 2,
          'timestamp': '2023-10-09T11:09:01.955',
          'observacoes': 'Objeto teste',
          'confidencial': false,
          'resumo_assunto': 'Assunto teste',
          'id_setor': 524,
          'andamentos': [
            {
              'cod_andamento': 1,
              'cod_processo': -1,
              'ano_exercicio': '2023',
              'cod_orgao': 2,
              'cod_unidade': 89,
              'cod_departamento': 2,
              'cod_setor': 1,
              'ano_exercicio_setor': '2003',
              'cod_usuario': 140050,
              'timestamp': '2023-10-09T11:09:01.955',
              'nome_setor_destino': 'TI - Tecnologia da Informação'
            }
          ],
          'atributosProtocolo': [
            {
              'cod_atributo': 1,
              'nom_atributo': 'Anotações',
              'tipo': 't',
              'valor_padrao': '',
              'valor': 'Anotações teste'
            }
          ],
          'nome_interessado': "Isaque Neves Sant'ana",
          'nom_assunto': 'Abono de permanência'
        }),
        headers: defaultHeaders..addAll(await getAuthorizationHeader(client)));

    expect(
        res,
        allOf([
          hasStatus(200),
          hasContentType('application/json'),
          hasValidBody(Validator({
            'cod_processo': isInt,
            'ano_exercicio': isEquals('2023'),
            'cod_classificacao': isEquals(169),
            'cod_assunto': isEquals(1),
            'numcgm': isEquals(140050),

     //here I simulate that I get a wrong value
            'cod_usuario': isEquals(14005025),          
          })),
        ]));
  });
}

the correct thing was to just throw a comparison error saying that x was expected but y received, right?

00:03 +0 -1: incluir processo [E]
  Expected: ....
    Actual: ....
  
  package:matcher                       expect
  test\incluir_processo_test.dart 78:5  main.<fn>

@insinfo This is a bug caused by describeMismatch not implemented and ValidationException not correctly handled. It is fixed in angel3_validate:8.0.2 and angel3_test:8.0.1. Validation for your test cases should work now.