GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — What's the best way to generate a fake response to write unit tests? · Issue #263 · comigor/artemis

This repository was archived by the owner on Oct 2, 2024. It is now read-only.
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

What's the best way to generate a fake response to write unit tests? #263

Copy link
Copy link
Open
Open
What's the best way to generate a fake response to write unit tests?#263
Copy link

Description

@GP4cK
GP4cK
opened on Feb 25, 2021
Issue body actions

Often, in my code, I have something like this:

Query(
  options: QueryOptions(
    document: ItemsQuery().document
    variables: ItemsArguments(...),
  ),
  builder: (result, { refetch, fetchMore }) {
    // skipping error handling etc.
    final data = ItemsQuery().parse(result.data);
    return MyWidget(data);
  },
);

When I want to write a test for MyWidget, I need to generate data to pass as an input. I can think of several solutions:

The first one is to manually create a json and do something like:

final json = [{
  'id': 'id0',
  'title': 'Title 0',
  'description': 'Description 0',
}, {
  'id': 'id1',
  // ...
}];
final data = ItemsQuery().parse(json);
await WidgetTestHelper.generateApp(tester, MyWidget(data));
expect(...);

This works ok but people may make mistakes when writing the JSON. I've already seen this happening in my team.

The other solution is to do something like:

final json = [
  (Items$Query$Item()
    ..id = 'id0'
    ..title = 'Title 0'
    ..description = 'Description 0').toJson()
];
final data = ItemsQuery().parse(json);
//...

This is slightly better because the editor will catch typos or attributes that do no exist.
But it gets really tedious when there are nested object in the response.
Ex:

Items$Query$Item()
  ..id = 'id0'
  ..title = 'Title 0'
  ..description = 'Description 0
  ..createdBy = (Items$Query$Item$CreatedBy()
    ..username = 'Alice'
    ..address = (Items$Query$Item$CreatedBy$Address() // <- this is very long
      // ...

Do you guys have a better way of doing this?

Reacted by Eranga Heshan

Activity

  1. comigor commented on Feb 28, 2021

    @comigor
    comigor
    on Feb 28, 2021
    Owner
    More actions

    Hi @GP4cK!

    In my work, for unit tests, we're doing your second example. Although kinda boring to write, it is surely safer than the first option.
    One of the teams is working on a code generator solution to mock those classes generated by Artemis, though. I'll reach them to see if they plan to ship it as opensource.

    Reacted by GP4cK, Alexey Ankip and Dwiky Hardiansah
  2. GP4cK commented on Feb 28, 2021

    @GP4cK
    GP4cK
    on Feb 28, 2021
    Author
    More actions

    Thanks a lot @comigor. Shall we close this now or we wait for the team's answer?

  3. comigor commented on Mar 1, 2021

    @comigor
    comigor
    on Mar 1, 2021
    Owner
    More actions

    I've send this link to them asking if they have any plans. Let's keep this open and I'll remember to update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.