GithubHelp home page GithubHelp logo

propicsignifi / t.apex Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 3.0 398 KB

Swiss knife for unit testing in Apex

Home Page: https://click-to-cloud.github.io/t-apex/

License: MIT License

Apex 100.00%
salesforce apex unit-testing bdd-style mocking test-data random

t.apex's Introduction

T.apex

T.apex is simply the Swiss knife for unit testing in Apex.

Why T.apex?

T.apex adopts expecting assertion style to make unit test codes more readable. Besides, T.apex provides powerful and easy mocking framework to make unit test easier. And T.apex has a built-in data generator to help you generate all kinds of random test data, including SObjects.

Dependencies

T.apex has a dependency on R.apex. Please include R.apex before getting started with T.apex.

Get Started

Expectations

T.expect(true).toBe(true);
T.expect(false).never.toBe(true);
T.expect(5).toEqual(5);
T.expect('abc').toMatch('.*b.*');
T.expect(null).toBeNull();
T.expect(true).toBeTrue();
T.expect(false).toBeFalse();
T.expect('abc').toContain('b');
T.expect(2).toBeLessThan(3);
T.expect(3).toBeGreaterThan(2);

Manual Failing

T.fail('Should fail here');

Create Mocks

Func mock = (Func)T.mock(Func.class);

Mock Method By Returning

T.when(mock.run(0)).thenReturn(0);
// When mock calls 'run' with 0, return 0

Mock Method By Throwing

T.when(mock.run(0)).thenThrow(new T.TestException('test'));
// When mock calls 'run' with 0, throw the exception

Mock Method By Answering

T.when(mock.run(0)).thenAnswer(R.inc);
// When mock calls 'run' with 0, apply the answer Func to the arguments
// and return the result

Argument Predicates

T.when(mock.run(T.anyBoolean(R.isNotNull))).thenReturn(0);
// When mock calls 'run' with any Boolean that is not null, return 0

Verify Mock Methods

mock.run(0);

T.verify(mock, 'run').toHaveBeenCalled();

Generate a Random Name

String name = (String)T.create('Name'); // Random person names

Generate a Random Sentence

String sentence = (String)T.create('Sentence'); // Random sentences

Generate a Random List of Strings

List<String> strList = (List<String>)T.create('List', new Map<String, Object>{
    'type' => 'String',
    'min' => 5
});
// Create a random list that contains at least 5 strings

Generate a Random SObject

Account acc = (Account)T.createSObject('Account', new Map<String, Object>{
    'fields' => new List<String>{ 'Description' }
});
// Create a random Account object that include 'Description' field

t.apex's People

Contributors

liumiaowilson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.