Giter Club home page Giter Club logo

approval_tests's Introduction

Approval Tests implementation in Dart ๐Ÿš€


Pub License: MIT Repository views Pub

Pub likes Pub popularity Pub points

โ—๏ธ Was moved to: https://github.com/approvals/ApprovalTests.Dart โ—๏ธ

๐Ÿ“– About

Unit testing asserts can be difficult to use. Approval tests simplify this by taking a snapshot of the results, and confirming that they have not changed.

In normal unit testing, you say expect(person.getAge(), 5). Approvals allow you to do this when the thing that you want to assert is no longer a primitive but a complex object. For example, you can say, Approvals.verify(person).

I am writing an implementation of a great tool like Approval Tests in Dart. If anyone wants to help, please text me. ๐Ÿ™

๐Ÿ“ฆ Installation

Add the following to your pubspec.yaml file:

dependencies:
  approval_tests: ^0.3.2-dev

๐Ÿ“š How to use

Approving Results

Approving results just means saving the .approved.txt file with your desired results.

Weโ€™ll provide more explanation in due course, but, briefly, here are the most common approaches to do this.

โ€ข Via Diff Tool

Most diff tools have the ability to move text from left to right, and save the result. How to use diff tools is just below, there is a Comparator class for that.

โ€ข Via approveResult property

If you want the result to be automatically saved after running the test, you need to use the approveResult property in Options:

test('test complex JSON object', () {
  var complexObject = {
    'name': 'JsonTest',
    'features': ['Testing', 'JSON'],
    'version': 0.1,
  };
  ApprovalTests.verifyAsJson(
    complexObject,
    options: const Options(
      approveResult: true,
    ),
  );
});

โ€ข Via file rename

You can just rename the .received file to .approved.

Comparators

You can use different comparators to compare files. The default is CommandLineComparator which compares files in the console.

CommandLineComparator img

To use IDEComparator you just need to add it to options:

 options: const Options(
   comparator: IDEComparator(
      ide: ComparatorIDE.visualStudioCode,
   ),
 ),

But before you add an IDEComparator you need to do the initial customization:

  • Visual Studio Code

    • For this method to work, you need to have Visual Studio Code installed on your machine.
    • And you need to have the code command available in your terminal.
    • To enable the code command, press Cmd + Shift + P and type Shell Command: Install 'code' command in PATH.
  • IntelliJ IDEA

    • For this method to work, you need to have IntelliJ IDEA installed on your machine.
    • And you need to have the idea command available in your terminal.
    • To enable the idea command, you need to create the command-line launcher using Tools - Create Command-line Launcher in IntelliJ IDEA.
  • Android Studio

    • For this method to work, you need to have Android Studio installed on your machine.
    • And you need to have the studio command available in your terminal.
    • To enable the studio command, you need to create the command-line launcher using Tools - Create Command-line Launcher in Android Studio.
Visual Studio code img Android Studio img

๐Ÿ“ Examples

I have provided a couple of small examples here to show you how to use the package. There are more examples in the example folder for you to explore. I will add more examples in the future. Inside, in the gilded_rose folder, there is an example of using ApprovalTests to test the legacy code of Gilded Rose kata. You can study it to understand how to use the package to test complex code.

And the verify_methods folder has small examples of using different ApprovalTests methods for different cases.

JSON example

import 'package:approval_tests/approval_dart.dart';
import 'package:test/test.dart';

void main() {
  test('Verify JSON output of an object', () {
    var item = Item(
      id: 1,
      name: "Widget",
      anotherItem: AnotherItem(id: 1, name: "AnotherWidget"),
      subItem: SubItem(
        id: 1,
        name: "SubWidget",
        anotherItems: [
          AnotherItem(id: 1, name: "AnotherWidget1"),
          AnotherItem(id: 2, name: "AnotherWidget2"),
        ],
      ),
    );

    ApprovalTests.verifyAsJson(
      item,
    );
  });
}

/// Item class for testing
class Item {
  final int id;
  final String name;
  final SubItem subItem;
  final AnotherItem anotherItem;

  Item({
    required this.id,
    required this.name,
    required this.subItem,
    required this.anotherItem,
  });
}

/// Sub item class for testing
class SubItem {
  final int id;
  final String name;
  final List<AnotherItem> anotherItems;

  SubItem({
    required this.id,
    required this.name,
    required this.anotherItems,
  });
}

/// Another item class for testing
class AnotherItem {
  final int id;
  final String name;

  AnotherItem({required this.id, required this.name});
}

Passed test example

โ“ Which File Artifacts to Exclude from Source Control

You must add any approved files to your source control system. But received files can change with any run and should be ignored. For Git, add this to your .gitignore:

*.received.*

โœ‰๏ธ For More Information

Questions?

Ask me on Telegram: @yelmuratoff.
Email: [email protected]

Video Tutorials

You can also watch a series of short videos about using ApprovalTests in .Net on YouTube.

Podcasts

Prefer learning by listening? Then you might enjoy the following podcasts:

๐Ÿค Contributing

Show some ๐Ÿ’™ and star the repo to support the project! ๐Ÿ™Œ
The project is in the process of development and we invite you to contribute through pull requests and issue submissions. ๐Ÿ‘
We appreciate your support. ๐Ÿซฐ



Thanks to all contributors of this package


approval_tests's People

Contributors

yelmuratoff avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.